Uploading XL Sheet data into SQL Server 2005 using C#:
This example will explain you how to upload the XL Sheet data into SQL Server 2005 using C#.
Add on upload control and button control to the ASP.NET 2.0 webform.
In button click event paste the following code:
protected void btn_upload1_Click(object sender, EventArgs e) { try { string FilePath = System.IO.Path.GetFullPath(fileload1.PostedFile.FileName); string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;"; using (OleDbConnection connection = new OleDbConnection(xConnStr)) { OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection); connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { string conStr = ConfigurationManager.AppSettings["conStr"]; using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conStr)) { bulkCopy.DestinationTableName = "Your_Table_Name"; bulkCopy.WriteToServer(dr); lbl_Hardwaremsg.Visible = true; lbl_Hardwaremsg.Text = "Hardware Details uploaded successfully"; } } } }
|
"Your_Table_Name" is your table name.
This is working in my application.
Ranjita MhatrePosted Aug 26, 2010, 6:36 AM
I am sweetu; thanks for this code
Easwaran ParamasivamPosted Aug 11, 2010, 3:11 AM
Nice one
MohammedPosted Jun 2, 2009, 1:28 AM
Good Job this kind of operation does not work with formatted excel sheet file because you have merged cells images and other staff. so u have to use special controls or using office automation to extract data from various cells that for sure not in the first row
Joe PoolPosted May 29, 2009, 2:19 PM
I've never seen anyone call an SQL Server using Microsoft.Jet. But seriously: Should the title say "Microsoft Access" or should the code be modified to access SQL Server? I am hesitant to consider the validity of the code when I see errors this quick in this short of an example.