Importing data from Excel to SQL database table
-
Tuesday, October 14, 2008 7:06 AM
Hi my friends,
I am developing an application. This application has SQL 2005 database. I want to enable the user to import data from Excel file to a SQL database table . I used this code, it worked fine but the data are not saved to the sql database when I click (save). Can you modify the code to enable data to be saved:
Private Sub loadDataFromExcel() Dim excelFile As String = "c:\new.xls" 'replace with valid path - file Dim excelSheet As String = "[Sheet$]" 'replace with valid Sheet Name Dim sql As String = "SELECT * FROM " & excelSheet ' add WHERE and ORDER if required Dim tableName As String = "student" Try Using cn As New OleDb.OleDbConnection ' HDR = YES if first row contain column name else NO ' Excel 8.0 / 9.0 ... depending on your Excel versioncn.ConnectionString =
"provider=Microsoft.Jet.OLEDB.4.0;" & _ "data source=" & excelFile & ";" & _ "Extended Properties=""Excel 8.0;HDR=YES""" Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, cn)
cn.Open()
da.Fill(
Me.MytestDataSet.student)
cn.Close()
End Using Catch ex As ExceptionMessageBox.Show(ex.Message)
End Try 'TODO proccess data to add / update other tables End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.ClickloadDataFromExcel()
End Sub
Thanks a lot- Edited by mishsad Tuesday, October 14, 2008 7:16 AM now better
All Replies
-
Monday, October 20, 2008 7:13 AM
Hi mishsad,
The above your code just retrieves/imports data from Excel file into DataSet/DataTable.
Here are some approaches/examples for you to check.
How to import data from Excel worksheet into SQL Server database?
1. http://support.microsoft.com/kb/321686
The samples in this article import Excel data by using:• SQL Server Data Transformation Services (DTS) • Microsoft SQL Server 2005 Integration Services (SSIS) • SQL Server linked servers • SQL Server distributed queries • ActiveX Data Objects (ADO) and the Microsoft OLE DB Provider for SQL Server • ADO and the Microsoft OLE DB Provider for Jet 4.0
2. Import Excel Spreadsheet Data into SQL Server Database Table Using SqlBulkCopy
http://davidhayden.com/blog/dave/archive/2006/05/31/2976.aspx
http://www.lockergnome.com/web/2006/05/31/import-excel-spreadsheet-data-into-sql-server-database-table-using-sqlbulkcopy/
3. How to import an Excel file into SQL Server 2005 using Integration Services
http://blogs.techrepublic.com.com/datacenter/?p=205
Best regards,
Martin Xie- Marked As Answer by Martin Xie - MSFT Monday, October 20, 2008 7:44 AM

