Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I use the below code, it does a bulk insert.
C#
string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath + ";extended properties=" + "\"excel 8.0;hdr=yes;\"";

string ssqlconnectionstring = "server=L96ZW52;userid=sa;password=test_123;database=master;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();

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;

My requirement is to stream data one by one and insert in a table. Please let me know what should be done for data streaming.
Posted
Updated 18-Dec-14 23:46pm
v3
Comments
Tomas Takac 19-Dec-14 5:50am    
Few notes:

1) The line where you actually call bulk copy is missing, but we can assume you use dr (OleDbDataReader) to source your data.

2) Did you hear about using and IDisposable?

3) You can truncate your staging table if there are no FKs.

4) What exactly is your problem? Why do you need to insert rows one by one?
Member 11322691 22-Dec-14 5:17am    
Hi Tomas,

I need to send data one by one in order to simulate a real time situation where the data is pushed in the server one by one not in bulk.
No I haven't used IDisposable. Will that help in data streaming?
Tomas Takac 22-Dec-14 7:42am    
So your question is how to write rows one by one using bulk copy? Set SqlBulkCopy.BatchSize[^] property to 1.

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