SqlClient CopyTableToServer Method Nemiro.Data.dll
Transferring the specified table to the database. Used SqlBulkCopy. The fastest and most economical way to move large amounts of data in the database.

Namespace: Nemiro.Data.Sql
Assembly: Nemiro.Data (in Nemiro.Data.dll) Version: 2.11.4.126 (2.11.4.126)
Syntax

public void CopyTableToServer(
	DataTable value
)

Parameters

value
Type: System.Data DataTable
Table whose you want transfer to the database.
Exceptions

ExceptionCondition
SqlExceptionThe exception that is thrown when SQL Server returns a warning or error.
StringOrBinaryDataWouldBeTruncatedExceptionAn exception occurs if some of the fields of the table are added values ​​that exceed the allowable size of a table field.
Remarks

Target table name should be in the property TableName instance of DataTable.

Fields list of instance the DataTable class must match to columns of the target table.

Caching options are ignored.

Examples

using (SqlClient client = new SqlClient())
{
  // Create DataTable instance. 
  // Table name is [hotels]. 
  // In SQL Server database should have a table named [hotels].
  DataTable table = new DataTable("hotels");
  // Add fields in the table.
  table.Columns.Add("hotel_code");
  table.Columns.Add("hotel_name");
  table.Columns.Add("hotel_stars");
  table.Columns.Add("date_created");
  // Random data generator, for example.
  Random rnd = new Random(DateTime.Now.Millisecond);
  for (int i = 0; i <= 1000; i++)
  {
    table.Rows.Add(Guid.NewGuid().ToString().Substring(0, 4), Guid.NewGuid().ToString().Replace("-", ""), rnd.Next(1, 5), DateTime.Now);
  }
  // Transfer data to SQL Server
  client.CopyTableToServer(table);
}
See Also