我有一个应用程序,它会做几个选择,然后是一些插入。可能会这样做大约200次,然后放下连接并关闭所有东西(据我所知)。这是代码:
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["FrameBudgetDB"].ToString()))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format("SELECT TOP 1 category_id FROM businesses WHERE '{0}' LIKE CONCAT('%',description,'%')", transDescription.Replace("'", "''"));
SqlDataReader reader = cmd.ExecuteReader();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
categoryId = (int)reader[0];
}
}
else
{
categoryId = 11; // Extgra Expenses
}
}
finally
{
reader.Close();
cmd.Dispose();
}
// Get Transaction Type
int transTypeId = 0;
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format("SELECT trans_type_id FROM transaction_types WHERE description = '{0}'", transType);
reader = cmd.ExecuteReader();
try
{
if (reader.HasRows)
{
while (reader.Read())
{
transTypeId = (int)reader[0];
}
}
}
finally
{
reader.Close();
cmd.Dispose();
}
try
{
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format(
"BEGIN " +
"IF NOT EXISTS(SELECT * FROM transactions " +
"WHERE transaction_date = @TransDate " +
"AND trans_type_id = @TransTypeId " +
"AND amount = @Amount " +
"AND ((from_bank = 1 AND description = @TransDesc) " +
"OR (from_bank = 0)) " +
"AND(category_id = @CategoryId OR previous_category_id = @CategoryId )) " +
"BEGIN " +
"INSERT INTO transactions(transaction_date, description, trans_type_id, import_date, category_id, amount, from_bank) " +
"VALUES(@TransDate, @TransDesc, @TransTypeId, @ImportDate, @CategoryId, @Amount, 1) " +
"END " +
"END");
cmd.Parameters.AddWithValue("@TransDate", transDate);
cmd.Parameters.AddWithValue("@TransDesc", transDescription);
cmd.Parameters.AddWithValue("@TransTypeId", transTypeId);
cmd.Parameters.AddWithValue("@Amount", amount);
cmd.Parameters.AddWithValue("@CategoryId", categoryId);
cmd.Parameters.AddWithValue("@ImportDate", DateTime.Now);
rowsInserted = rowsInserted + cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
conn.Close();
conn.Dispose();
}
scope.Complete();
scope.Dispose();
}
}
在所有插入之后,我想通过复制并将其移动到其他地方来备份数据库,但是sqlserver.exe会长时间锁定文件。我已经尝试让应用程序等待,但它似乎持有很长时间的锁定。
我已经构建了一个锁定检查while循环,但是知道实际需要多长时间会很好吗?
答案 0 :(得分:1)
似乎执行插入的代码的最后部分存在问题。
因为SqlCommand位于try..catch内的最后一行,所以如果发生异常,它可能根本不会执行。
最好在内部使用单独的SqlCommands来确保正确处理所有内容。 这可能是连接保持打开的原因。 你的代码:
try
{
cmd = new SqlCommand();
cmd.Connection = conn;
...
rowsInserted = rowsInserted + cmd.ExecuteNonQuery();
cmd.Dispose(); // <- This line might not be executed if exception occurs in previous line
}
建议更正:
try
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
...
rowsInserted = rowsInserted + cmd.ExecuteNonQuery();
// Call dispose is not needed
}
}