Bulk Insert

connString is your database connection string
dt is the bulk data which has to be uploaded
table is the table name of you database
public void BulkInsert(string connString, DataTable dt, string Table)
        {
            if (connString == null || connString.Length == 0) throw new ArgumentNullException("connectionString");

            using (SqlConnection connection =
                    new SqlConnection(connString))
            {
                SqlBulkCopy bulkCopy =
                    new SqlBulkCopy
                    (
                    connection,
                    SqlBulkCopyOptions.TableLock |
                    SqlBulkCopyOptions.FireTriggers |
                    SqlBulkCopyOptions.UseInternalTransaction,
                    null
                    );

                // set the destination table name
                bulkCopy.DestinationTableName = Table;
                connection.Open();

                // write the data in the "dataTable"
                bulkCopy.WriteToServer(dt);
                connection.Close();
            }
        }

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More