Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

It looks much cleaner with the TransactionScope.

using (TransactionScope scope = new TransactionScope())
using (var connection = new SqlConnection(GetConnectionString()))
{
 connection.Open();
 try
 {
 foreach (var fileData in Helpers.StreamFile(filePath, tableName, fieldSeparator, numberOfSourceColumns))
 {
 // BulkInsert uses SqlBulkCopy to insert data.
 Helpers.BulkInsert(fileData.Data, tableName, connection, transaction);
 }
 scope.Complete();
 return true;
 }
 catch (Exception)
 {
 return false;
 }
}

You should also read this: Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy? Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

The last parameter is unnecessary.


Exception handling

Are you sure you want to ignore the exception? I guess you log it somehow, do you?

It looks much cleaner with the TransactionScope.

using (TransactionScope scope = new TransactionScope())
using (var connection = new SqlConnection(GetConnectionString()))
{
 connection.Open();
 try
 {
 foreach (var fileData in Helpers.StreamFile(filePath, tableName, fieldSeparator, numberOfSourceColumns))
 {
 // BulkInsert uses SqlBulkCopy to insert data.
 Helpers.BulkInsert(fileData.Data, tableName, connection, transaction);
 }
 scope.Complete();
 return true;
 }
 catch (Exception)
 {
 return false;
 }
}

You should also read this: Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

The last parameter is unnecessary.


Exception handling

Are you sure you want to ignore the exception? I guess you log it somehow, do you?

It looks much cleaner with the TransactionScope.

using (TransactionScope scope = new TransactionScope())
using (var connection = new SqlConnection(GetConnectionString()))
{
 connection.Open();
 try
 {
 foreach (var fileData in Helpers.StreamFile(filePath, tableName, fieldSeparator, numberOfSourceColumns))
 {
 // BulkInsert uses SqlBulkCopy to insert data.
 Helpers.BulkInsert(fileData.Data, tableName, connection, transaction);
 }
 scope.Complete();
 return true;
 }
 catch (Exception)
 {
 return false;
 }
}

You should also read this: Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

The last parameter is unnecessary.


Exception handling

Are you sure you want to ignore the exception? I guess you log it somehow, do you?

Source Link
t3chb0t
  • 44.6k
  • 9
  • 84
  • 190

It looks much cleaner with the TransactionScope.

using (TransactionScope scope = new TransactionScope())
using (var connection = new SqlConnection(GetConnectionString()))
{
 connection.Open();
 try
 {
 foreach (var fileData in Helpers.StreamFile(filePath, tableName, fieldSeparator, numberOfSourceColumns))
 {
 // BulkInsert uses SqlBulkCopy to insert data.
 Helpers.BulkInsert(fileData.Data, tableName, connection, transaction);
 }
 scope.Complete();
 return true;
 }
 catch (Exception)
 {
 return false;
 }
}

You should also read this: Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

The last parameter is unnecessary.


Exception handling

Are you sure you want to ignore the exception? I guess you log it somehow, do you?

lang-cs

AltStyle によって変換されたページ (->オリジナル) /