Sunday, May 24, 2009

String Connection To Upload Data From Excel To SQL Using ASP.NET VB

Sometimes we would like to upload mass data from excel to SQL 2005.
Here the connection string :

Dim FileLocation As String = "D:\Wihemdra\Book1.xls"

' Create connection string variable. Modify the "Data Source" parameter as' appropriate for your environment.

Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _"Data Source=" & FileLocation & ";" & _"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

' Create the connection object by using the preceding connection string.
Dim objConn As New OleDbConnection(sConnectionString)

' Open connection with the database.
objConn.Open()

' The code to follow uses a SQL SELECT command to display the data from the worksheet.
' Create new OleDbCommand to return data from worksheet.
Dim objCmdSelect As New OleDbCommand("SELECT * FROM [Sheet1$]", objConn)

' Create new OleDbDataAdapter that is used to build a DataSet' based on the preceding SQL SELECT statement.
Dim objAdapter1 As New OleDbDataAdapter()

' Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect

' Create new DataSet to hold information from the worksheet.
Dim objDataset1 As New Data.DataSet()

' Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData")

' Clean up objects.
objConn.Close()

No comments:

Post a Comment