Click here to Skip to main content
16,014,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read an excel sheet data into datagridview with sql database connection.

Please support.
Posted
Updated 1-Dec-15 21:36pm
v2

1 solution

hi!
Do you need to Save Excel to Sql or Sql data to Excel?
 
Share this answer
 
Comments
Member 11745361 2-Dec-15 3:43am    
I want Import excel sheet data into datadgrid in C#. connection string is sql.
kasunthilina 16-Dec-15 6:50am    
sorry for the late.
here is the code to add excel to datagridview then try to save each datagrid line to sql db.

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As New DataTable
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & OpenFileDialog1.FileName & "';Extended Properties=""Excel 8.0;IMEX=1""")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [SHEET NAME$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")

MyCommand.Fill(DtSet)
DATAGRIDVIEW.DataSource = DtSet
MyConnection.Close()
Ageresa 17-Mar-16 7:08am    
You cannot use SQL connection for this, but you can use OLEDB connection.
Another way that you can use is this code to read an excel file in C#, also this way can provide you a direct method for exporting excel's sheet into a DataGridView control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900