Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi! how the BULK INSERT statement works explain with code.
Posted
Comments
OriginalGriff 13-Apr-12 3:18am    
Reason for my vote of one: too lazy to google
santosh_k 16-Apr-12 21:15pm    
http://insqlserver.com/comment/4
santosh_k 16-Apr-12 21:36pm    
http://wikiprogrammer.wordpress.com/2012/02/24/load-excel-file-dynamically-into-database-using-sqlbulkcopy-and-getoledbschematable-in-c/
santosh_k 16-Apr-12 23:52pm    
http://stackoverflow.com/questions/9415560/sql-bulk-copy-from-data-table-will-not-upload

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your subject as the search term gave over 2 1/2 million hits.
The top hit is MSDN: http://msdn.microsoft.com/en-us/library/ms188365.aspx[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
From: http://www.eggheadcafe.com/community/sql-server/13/10351461/how-to-import-excel-data-to-a-sql-table.aspx[^]

2. Now design a tStudent table in SQL server
SQL
Create Table 
( 
   StudentName varchar(64), 
   RollNo varchar(16), 
   Course varchar(32), 
) 

your ms excel sheet and SQL table is ready, now its time to write c# code to import the excel sheet intotStudent table

3.
Add these two name space in your class file
C#
using System.Data.OleDb;
using System.Data.SqlClient;

Use following code
C#
public void importDataFromExcel(string excelFilePath)
{ 
   //Declare Variables - Edit these based on your particular situation
   string sSQLTable = "tDataMigrationTable";
   
   // make sure your sheet name is correct, here sheet name is Sheet1, so you can change your sheet name if have different
   string myExcelDataQuery = "Select StudentName,RollNo,Course from [Sheet1$]"; 

   try
   {
      //Create our connection strings
      string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
      string sSqlConnectionString = "SERVER=MyDatabaseServerName;USER ID=DBUserId;PASSWORD=DBUserPassword;DATABASE=DatabaseName;CONNECTION RESET=FALSE"; 

      //Execute a query to erase any previous data from our destination table
      string sClearSQL = "DELETE FROM " + sSQLTable;
      SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
      SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
      SqlConn.Open();
      SqlCmd.ExecuteNonQuery();
      SqlConn.Close(); 

      //Series of commands to bulk copy data from the excel file into our SQL table
      OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
      OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn); 
      OleDbConn.Open(); 
      OleDbDataReader dr = OleDbCmd.ExecuteReader();
      SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString); 
      bulkCopy.DestinationTableName = sSQLTable; 
      while (dr.Read())
      {
         bulkCopy.WriteToServer(dr);
      } 
      OleDbConn.Close();
   }
   catch (Exception ex)
   {
      //handle exception
   } 
}
 
Share this answer
 
v6
Comments
fjdiewornncalwe 25-Apr-12 13:29pm    
I would suggest that in the future when you copy/paste an answer from another source that is not your own, you cite the source.
I have taken the liberty of adding that to your answer.

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