export excel data into sql server using c#
Posted by Sujit Kumar on September 6, 2008
Condition :
excel file columns and sql server tables columns should be same and datatype also should be same.
public void Exel2Sql()
{
OdbcConnection connection;
SqlBulkCopy bulkCopy;
string ConnectionString = @”server=sujitkumar\sqlexpress;database=pubs;uid=sa;pwd=1234;”;
string connstr = @”Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=c:\contact.xls”;
using (connection = new OdbcConnection(connstr))
{
OdbcCommand command = new OdbcCommand(“Select * FROM [Sheet1$]“, connection);
//you can change [Sheet1$] with your sheet name
connection.Open();
// Create DbDataReader to Data Worksheet
using (OdbcDataReader dr = command.ExecuteReader())
{
// Bulk Copy to SQL Server
using (bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.DestinationTableName = “Names”;
//”Names” is the sql table where you want to copy all data.
bulkCopy.WriteToServer(dr);
}
dr.Close();
}
}
bulkCopy.Close();
connection.Close();
}
basanth said
how can we know the datatype of excel column?In one of my column i have these values ie
1234
ASD-123-AXS
2345
Eselk-1cd
wsax
wewe-12343
If i export these values few cells where inserted like nulls..I tried for these datatypes ie Varchar(50),Varchar(Max),nvarchar(50),nvarchar(Max)..Please resolve my quesion soon..
Thanks
Basanth
RathikaKrishnavelu said
Hi Sujit,
I have implemented your code in my application. The problem is in Excel the field is rounded up. For example the actual value of the cell is : 7894.71987. But in Excel itself it is rounded as 7894.72. If i put ‘ symbol infront of the value, its getting uploaded fine with the original value. But we cant ask the client to put ‘. Kindly gimme solution. Th datatypes in Excel, Table is Varchar only.
Thanks in Advance,
RathikaKrishnavelu
Sujit Kumar said
try to format the excel cell’s value and then try to submit data in sql from excel.
because there is some limitation while you export data from excel to sql.
RathikaKrishnavelu said
Hi Sujit,
Thanx for ur reply. I tried formatting the cells as text. Still in the excel itself it is rounded off.
I’ve created a Excel template with Macros. The client also accepted. Thanx sujit.
Thanks,
RathikaKrishnavelu
tor said
I got following error !
ERROR [HY000] [Microsoft][ODBC Excel Driver] Cannot open database ‘(unknown)’. It may not be a database that your application recognizes, or the file may be corrupt.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed
ERROR [HY000] [Microsoft][ODBC Excel Driver] Cannot open database ‘(unknown)’. It may not be a database that your application recognizes, or the file may be corrupt.
Sujit Kumar said
What is your connectionstring, Please check you your connection string.
try to use this connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.xls;Extended Properties=”"Excel 8.0;HDR=YES;;
for more info about connectionstring please visit this link:
connectionstring
or check may be your file would be corrupt.
sandrar said
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.