Joseph Jones
asked on
Import Excel data into SQL Server 2005 though vb.net
Hi,
I need to import excel spread sheets in to sql server 2005 tables through front end application (VB.NET 2008) and show the data in a datagridview. I want to have a best method to do it. If anybody can help me on this, it would be great.
Thanks
JOLEJEJE
I need to import excel spread sheets in to sql server 2005 tables through front end application (VB.NET 2008) and show the data in a datagridview. I want to have a best method to do it. If anybody can help me on this, it would be great.
Thanks
JOLEJEJE
ASKER
Hi om prakash,
I spent hours to fix the problem of isam error in excel. However, I fixed it but now I get the following error message "Syntax Error in FROM Clause" while filling the dataset. I am not used C# and therefore, I had spent time also on converting the code to VB. It is Ok. Following is my vbcode. Please check where I have gone wrong.
Public Sub JoeImportExcel(ByVal strFile)
If Not File.Exists(strFile) = True Then
MessageBox.Show("File does not exist...!!!")
Exit Sub
End If
Dim JoeExcelConString As String
'JoeExcelConString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=C:\TEST\TESTING.XLS ;Extended Properties=Excel 11.0;"
JoeExcelConString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=" & strFile & ";Extended Properties=""Excel 8.0;HDR=YES;"""
Dim SheetName As String = "Sheet1"
Dim JoeExcelCon As New OleDbConnection
JoeExcelCon.ConnectionStri ng = JoeExcelConString
Try
JoeExcelCon.Open()
Dim JoeExcelAda As New OleDbDataAdapter("SELECT * FROM Sheet1$", JoeExcelCon)
Dim ExcelData As New DataSet()
JoeExcelAda.Fill(ExcelData )
Catch Olex As OleDb.OleDbException
MessageBox.Show("Err No: " & Olex.ErrorCode & " Err Msg: " & Olex.Message, "Excel Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If JoeExcelCon.State = ConnectionState.Open Then
JoeExcelCon.Close()
End If
End Try
End Sub
Once I get success in this code, then I may be able to populate them in a datagridview.
Please help me on this issue.
JOLEEJEJE
I spent hours to fix the problem of isam error in excel. However, I fixed it but now I get the following error message "Syntax Error in FROM Clause" while filling the dataset. I am not used C# and therefore, I had spent time also on converting the code to VB. It is Ok. Following is my vbcode. Please check where I have gone wrong.
Public Sub JoeImportExcel(ByVal strFile)
If Not File.Exists(strFile) = True Then
MessageBox.Show("File does not exist...!!!")
Exit Sub
End If
Dim JoeExcelConString As String
'JoeExcelConString = "Provider=Microsoft.Jet.OL
JoeExcelConString = "Provider=Microsoft.Jet.OL
Dim SheetName As String = "Sheet1"
Dim JoeExcelCon As New OleDbConnection
JoeExcelCon.ConnectionStri
Try
JoeExcelCon.Open()
Dim JoeExcelAda As New OleDbDataAdapter("SELECT * FROM Sheet1$", JoeExcelCon)
Dim ExcelData As New DataSet()
JoeExcelAda.Fill(ExcelData
Catch Olex As OleDb.OleDbException
MessageBox.Show("Err No: " & Olex.ErrorCode & " Err Msg: " & Olex.Message, "Excel Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If JoeExcelCon.State = ConnectionState.Open Then
JoeExcelCon.Close()
End If
End Try
End Sub
Once I get success in this code, then I may be able to populate them in a datagridview.
Please help me on this issue.
JOLEEJEJE
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.knowdotnet.com/articles/exceldatasource.html
Display data from sql in datagridview can be something like this:
Open in new window