.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Import Excel Data into SQLServer

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

This code chows how to import a excel sheet data into sql server

string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=F:\\name\\WebSite\\WebSite5\\attendance.xls;" +
"Extended Properties=Excel 8.0;";


OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = strConn;
conn.Open();
OleDbCommand Olecmd = new OleDbCommand("select * from [Sheet1$]", conn );
drReader = Olecmd.ExecuteReader();
drReader.Read();
SqlConnection conn1 = new SqlConnection();
conn1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["strConn"];
conn1.Open();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
bulkCopy.DestinationTableName = "tablename";

bulkCopy.WriteToServer(drReader);
Response.Write("Loaded");

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend