SqlBulkCopy
-
Not sure if it is a Sql issue or a syntax error of sorts so here it is. I have a DataTable with a single column Player Name. It is filled with some 20,000 rows. I need to insert this into a Sql database. I have a Sql database setup with a table called Unknown, it has 2 columns, an Indentity column ID, and a varchar(50) column Player Name. I copied the data over one-by-one and it took like 15-30 minutes to do, not I am using SqlBulkCopy. The problem is, it simply isn't working. No error, no warnings, no nothing. I have SQL Profiler open and don't see it even hitting the database. Code is attached, what am I doing wrong?
SqlConnection connection = new SqlConnection(Properties.Settings.Default.TibiaSQLConnection);
connection.Open(); SqlBulkCopy copier = new SqlBulkCopy(connection); copier.BatchSize = 500; copier.NotifyAfter = 1000; copier.SqlRowsCopied += new SqlRowsCopiedEventHandler(copier\_SqlRowsCopied); copier.DestinationTableName = "Unknown"; copier.WriteToServer(dtUnknowns); copier.Close(); connection.Close();
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
-
Not sure if it is a Sql issue or a syntax error of sorts so here it is. I have a DataTable with a single column Player Name. It is filled with some 20,000 rows. I need to insert this into a Sql database. I have a Sql database setup with a table called Unknown, it has 2 columns, an Indentity column ID, and a varchar(50) column Player Name. I copied the data over one-by-one and it took like 15-30 minutes to do, not I am using SqlBulkCopy. The problem is, it simply isn't working. No error, no warnings, no nothing. I have SQL Profiler open and don't see it even hitting the database. Code is attached, what am I doing wrong?
SqlConnection connection = new SqlConnection(Properties.Settings.Default.TibiaSQLConnection);
connection.Open(); SqlBulkCopy copier = new SqlBulkCopy(connection); copier.BatchSize = 500; copier.NotifyAfter = 1000; copier.SqlRowsCopied += new SqlRowsCopiedEventHandler(copier\_SqlRowsCopied); copier.DestinationTableName = "Unknown"; copier.WriteToServer(dtUnknowns); copier.Close(); connection.Close();
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions
-
copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions
It all is wrapped in a try except. That was just a snippit. Let me try that and see how it works. Do you have to do that even when both columns have the same name?
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
-
copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions
That worked great, thanks a ton.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo