我正在使用事务运行多个插入操作。我使用SqlDependency类让客户端机器知道服务器何时已更新。
我的问题是,无论我为事务设置什么隔离级别,每当我使用事务进行插入时,SqlNotificationEventArgs都会将e.Info返回为Isolation,这表明我为该事务设置了错误的隔离级别(我想)。如果我不使用事务进行插入,则一切顺利运行。
我的问题是,在使用Sql Notification时,是否支持任何事务的隔离级别?
以下是我用于通知的部分代码:
并且针对交易:
我的问题是,无论我为事务设置什么隔离级别,每当我使用事务进行插入时,SqlNotificationEventArgs都会将e.Info返回为Isolation,这表明我为该事务设置了错误的隔离级别(我想)。如果我不使用事务进行插入,则一切顺利运行。
我的问题是,在使用Sql Notification时,是否支持任何事务的隔离级别?
以下是我用于通知的部分代码:
void DataChanged(object sender, SqlNotificationEventArgs e) {
var i = (ISynchronizeInvoke)_form;
if (i.InvokeRequired) {
var tempDelegate = new OnChangeEventHandler(DataChanged);
object[] args = { sender, e };
i.BeginInvoke(tempDelegate, args);
} else {
var dependency = (SqlDependency)sender;
if (e.Type == SqlNotificationType.Change) {
dependency.OnChange -= DataChanged;
GetData(dependency);
}
}
}
并且针对交易:
public void ExecuteNonQueryData(List<string> commandTexts) {
SqlConnection connection = null;
var command = new SqlCommand();
SqlTransaction transaction = null;
try {
connection = new SqlConnection(GetConnectionString());
connection.Open();
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
foreach (var commandText in commandTexts) {
try {
command.Connection = connection;
command.CommandText = commandText;
command.Transaction = transaction;
command.ExecuteNonQuery();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
transaction.Commit();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} finally {
command.Dispose();
if (transaction != null) transaction.Dispose();
if (connection != null) {
connection.Close();
connection.Dispose();
}
}
commandTexts.Clear();
}
编辑:我在错误的位置提交了事务。