running sqlbulkcopy in c# and I get an error: WriteToServer: Connection property has not been initialized.
it happens at the WriteToServer command. The connection is open.
using (SqlBulkCopy s = new SqlBulkCopy(conn))
{
foreach (DataTable dt in ds.Tables)
{
s.DestinationTableName = "tmp_" + dt.TableName;
s.NotifyAfter = 5000;
s.SqlRowsCopied += new SqlRowsCopiedEventHandler(s_SqlRowsCopied);
s.WriteToServer(dt);
s.Close();
}
}
Correct Code:
using (SqlBulkCopy s = new SqlBulkCopy(conn))
{
foreach (DataTable dt in ds.Tables)
{
s.DestinationTableName = "tmp_" + dt.TableName;
s.NotifyAfter = 5000;
s.SqlRowsCopied += new SqlRowsCopiedEventHandler(s_SqlRowsCopied);
s.WriteToServer(dt);
}
s.Close();
}