By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

how to insert data from a data table into sqlserver table

P: 9
hi, i have to enter data from a delimited file into sqlserver database table.
i have been able to delimit the file and read the data into a data table, now i want enter the data table contents to sqlserver. some help in this matter...if possible code for moving data from data table to the database....would be really helpful. thanks in advance.
Jul 25 '10 #1

✓ answered by ThatThatGuy

@indona
http://www.jonasjohn.de/snippets/csh...on-example.htm

I think you should follow this tutorial

Share this Question
Share on Google+
4 Replies


ThatThatGuy
Expert 100+
P: 449
@indona
http://www.jonasjohn.de/snippets/csh...on-example.htm

I think you should follow this tutorial
Jul 26 '10 #2

P: 22
if you already have data in your DataTable it just takes
4 steps to update into database

1)create a connection to database:

con = new SqlConnection(conString);
// for conString see
http://www.csharp-station.com/Tutori.../Lesson02.aspx


2)Open connection :

con.Open();


3)Enter into DataBase with a loop like :

for(int i=0;i<dt.Rows.Count;i++)
{
command = new SqlCommand("INSERT INTO tables (value,val) VALUES('"+dt.Rows[i][0].ToString()+"','"+dt.Rows[i][1].ToString()+"')",con);
command.ExecuteNonQuery();
}

here i assume two columns so

dt.Rows[i][0].ToString() is the string from row i and column 0
dt.Rows[i][1].ToString() is the string from row i and column 1

u have to pass values to your own need

4)Close Connection :
con.Close();
Jul 26 '10 #3

P: 9
thanks a lot....the tutorial and the code you provided really helped..n wid that have been able to do my work..
thanks a lot all..
Jul 26 '10 #4

P: 1
Try this:
Expand|Select|Wrap|Line Numbers
  1. private void BulkUpload(DataTable dt)
  2. {
  3.     dt.TableName="YourDataTable";
  4.     string constr="your connection string";
  5.     using(SqlConnection connection=new SqlConnection(constr))
  6.     {
  7.       connection.Open();
  8.       //CreatingTranscationsothatitcanrollbackifgotanyerrorwhileuploading
  9.       SqlTransaction trans=connection.BeginTransaction();
  10.      //Start bulkCopy
  11.      using(SqlBulkCopy bulkCopy=new SqlBulkCopy(connection,
  12.      SqlBulkCopyOptions.TableLock|
  13.      SqlBulkCopyOptions.FireTriggers,
  14.      trans))
  15.      {
  16.        //Setting timeout to 0 means no time out for this command will not timeout until upload complete.
  17.      //Change as per you
  18.      bulkCopy.BulkCopyTimeout=0;
  19.      bulkCopy.DestinationTableName=dt.TableName;
  20.      //write the data in the "dataTable"
  21.      bulkCopy.WriteToServer(dt);
  22.     }
  23.    }
  24. }
  25.  
From:

Bulk insert or upload records into SQL Server with DataTable in c sharp using SqlBulkCopy
Feb 21 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.