从 SqlExpress 数据库上的代码执行时,是否有人知道使用 SqlBulkCopy 的问题?
我有一个完整的 SQL Server 2008 实例和一个 SQL Server 2008 Express 实例。完整实例安装在服务器上,快速实例安装在本地测试机器上。每当我使用服务器连接字符串运行我的应用程序(使用以下代码)时,它执行得很好,我可以看到新插入的行和信息性消息。但是,每当我使用 Local Sql Express 连接字符串运行我的应用程序时,什么都没有发生,也没有收到任何消息,也没有插入表行。这是安装在测试机器上的桌面应用程序(运行 Sql Express 的同一台机器)
这真的让我很难过,因为我已经在 try catch 块中得到了所有内容,但没有抛出异常。似乎它没有执行。请指教!谢谢
这是我的代码:
public static bool BulkInsertReminders(DataTable dtReminders)
{
bool success = false;
string sql = "user id=" + DBEntries.DBUser + ";password=" + DBEntries.DBPassword + ";server=" + DBEntries.DBHost + ";database=" + DBEntries.LocalDB + ";Encrypt=YES;TrustServerCertificate=true";
MessageBox.Show("Insert Rows Count: " + dtReminders.Rows.Count);
using (SqlConnection msSQL = new SqlConnection(sql))
{
SqlTransaction copy = null;
SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + "dbo.reminders", msSQL);
try
{
msSQL.Open();
copy = msSQL.BeginTransaction();
using (SqlBulkCopy load = new SqlBulkCopy(msSQL, SqlBulkCopyOptions.TableLock, copy))
{
load.BulkCopyTimeout = 15;
load.ColumnMappings.Add("reminder_id", "reminder_id");
load.ColumnMappings.Add("creation_date", "creation_date");
load.ColumnMappings.Add("created_by", "created_by");
load.ColumnMappings.Add("sent", "sent");
load.ColumnMappings.Add("sent_by", "sent_by");
load.ColumnMappings.Add("sent_date", "sent_date");
load.DestinationTableName = "dbo.reminders";
load.WriteToServer(dtReminders);
success = true;
commandRowCount.Transaction = copy;
int totalRows = Convert.ToInt32(commandRowCount.ExecuteScalar());
MessageBox.Show("New Table Row Count: " + totalRows + Environment.NewLine + "Rows Before Insert: " + (totalRows - dtReminders.Rows.Count));
}
}
catch (Exception exc)
{
MessageBox.Show("Error - Issues adding reminders information to database.", "Contact Support");
email err = new email();
err.sendErrorEmail(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), exc.Message, exc.Data, exc.StackTrace);
success = false;
copy.Rollback();
}
}
return success;
}