how to insert data from a data table into sqlserver table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indona
    New Member
    • Jul 2010
    • 9

    #1

    how to insert data from a data table into sqlserver table

    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....wou ld be really helpful. thanks in advance.
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by indona
    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....wou ld be really helpful. thanks in advance.


    I think you should follow this tutorial

    Comment

    • maddyromeo
      New Member
      • Jul 2010
      • 22

      #3
      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(c onString);
      // for conString see
      SqlConnection objects let other ADO.NET codes know what database to connect to and how to make the connection. Check it now!



      2)Open connection :

      con.Open();


      3)Enter into DataBase with a loop like :

      for(int i=0;i<dt.Rows.C ount;i++)
      {
      command = new SqlCommand("INS ERT INTO tables (value,val) VALUES('"+dt.Ro ws[i][0].ToString()+"', '"+dt.Rows[i][1].ToString()+"') ",con);
      command.Execute NonQuery();
      }

      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();

      Comment

      • indona
        New Member
        • Jul 2010
        • 9

        #4
        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..

        Comment

        • samundersingh19
          New Member
          • Feb 2014
          • 1

          #5
          Try this:
          Code:
          private void BulkUpload(DataTable dt)
          {
              dt.TableName="YourDataTable";
              string constr="your connection string";
              using(SqlConnection connection=new SqlConnection(constr))
              {
                connection.Open();
                //CreatingTranscationsothatitcanrollbackifgotanyerrorwhileuploading
                SqlTransaction trans=connection.BeginTransaction();
               //Start bulkCopy
               using(SqlBulkCopy bulkCopy=new SqlBulkCopy(connection,
               SqlBulkCopyOptions.TableLock|
               SqlBulkCopyOptions.FireTriggers,
               trans))
               {
                 //Setting timeout to 0 means no time out for this command will not timeout until upload complete.
               //Change as per you
               bulkCopy.BulkCopyTimeout=0;
               bulkCopy.DestinationTableName=dt.TableName;
               //write the data in the "dataTable"
               bulkCopy.WriteToServer(dt);
              }
             }
          }
          From:

          Bulk insert or upload records into SQL Server with DataTable in c sharp using SqlBulkCopy

          Comment

          Working...