I am trying to use SqlBulkCopy in order to insert plenty of rows in short time. I get the error:"
Cannot access destination table myTable.
Here is my code:
string conn = ConfigurationManager.ConnectionString["myConnection"].ToString();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(int)));
for (int i=0;i<10000;i++)
{
DataRow dr = dt.NewRow();
dr["Col1"] = "Col1_" + i.toString();
dr["Col2"] = i;
}
using (SqlBulkCopy bulkSql = new SqlBulkCopy(conn))
{
bulkSql.BatchSize = 10000;
bulkSql.BulkCopyTimeout = 10000;
bulkSql.ColumnMappings.Clear();
bulkSql.ColumnMappings.Add("Col1", "Col1");
bulkSql.ColumnMappings.Add("Col2", "Col2");
bulkSql.DestinationTableName = "[myTable]"
bulkSql.WriteToServer(dt);
}