如何解决.Net 使用事务和 SqlClient 的准备语句
我正在尝试为脚本实现事务,但遇到了一个奇怪的问题。
当我尝试在事务中运行准备好的 sql 语句时,它失败了,因为它说在分配连接时需要一个事务。
这如何与预处理语句配合使用,因为我打算让多个事务都使用相同的预处理语句。
我的代码如下
class dbTest {
public static sqlConnection db;
public static sqlCommand query;
static void Main(string[] args) {
db = connect();
prepare();
transaction01();
transaction02();
transaction03();
}
public static void prepare() {
query = new sqlCommand("select id from table where id = 1 for update",db);
query.Prepare();
}
public static void transaction01() {
sqlTransaction trans = db.BeginTransaction("Trn01");
sqlDataReader result = query.ExecuteReader();
while(result.Read()) { Console.WriteLine(result["id"]); }
result.Close();
trans.Commit();
}
public static void transaction02() {
sqlTransaction trans = db.BeginTransaction("Trn02");
sqlDataReader result = query.ExecuteReader();
while(result.Read()) { Console.WriteLine(result["id"]); }
result.Close();
trans.Commit();
}
public static void transaction03() {
sqlTransaction trans = db.BeginTransaction("Trn03");
sqlDataReader result = query.ExecuteReader();
while(result.Read()) { Console.WriteLine(result["id"]); }
result.Close();
trans.Commit();
}
}
如何将交易分配给现有的准备好的报表?
更新
更改了上面的代码以更好地显示问题。 sql 已准备好一次,但我会将它用于多个事务(或至少我想)
再次更新
我已将下面的一个答案标记为正确答案,因为它看起来是实现这一目标的最佳方法,但在这个非常小的示例中,我的需要使用 query.Transaction
使其正常工作
public static void transaction01() {
sqlTransaction trans = db.BeginTransaction("Trn01");
query.Transaction = trans; // this line fixed it
sqlDataReader result = query.ExecuteReader();
while(result.Read()) { Console.WriteLine(result["id"]); }
result.Close();
trans.Commit();
}
解决方法
-
使用 SqlTransaction 时,您必须显式设置 SqlCommand.Transaction,即使在 SQL Server 中登记当前事务不是可选的。
-
select ... for update
不是有效的 SQL Server 语法,而是使用 UPDLOCK 读取表并在事务期间保留限制性锁。 EGselect id from table with (updlock) where id = 1
当我尝试运行准备好的 SQL 语句时
- 在 SQL Server 中使用准备好的语句很少有用。即使没有它,查询计划缓存也会自动发生,当您使用不同的参数多次执行 SqlCommand 时,它实际上只是减少了网络上请求的大小。
但是准备好的 SqlCommand 仍然绑定到单个 SqlConnection,它的生命周期通常很短,从而最大限度地减少了准备 SqlCommand 的潜在好处。
,您需要将 SqlCommand.Transaction
设置为您的交易对象。
SQL Server 不需要准备语句。继续执行。
另请注意,正如您在 this post 中看到的那样,您必须正确处理所有数据库对象。
这是您清理的代码:
class dbTest {
// DO NOT cache connection object
static void Main(string[] args) {
using(var db = connect())
using(var comm = GetCommand(db))
{
transaction01(comm);
transaction02(comm);
transaction03(comm);
}
}
public static SqlCommand GetCommand(SqlConnection conn) {
return new SqlCommand("select id from table with (updlock) where id = 1",conn);
}
public static void transaction01(SqlCommand comm) {
using(SqlTransaction trans = comm.Connection.BeginTransaction("Trn01"))
{
comm.Transaction = trans;
using(SqlDataReader result = query.ExecuteReader())
while(result.Read()) { Console.WriteLine(result["id"]); }
trans.Commit();
} // no need to close,using will sort that out
}
public static void transaction02(SqlCommand comm) {
using(SqlTransaction trans = comm.Connection.BeginTransaction("Trn02"))
{
comm.Transaction = trans;
using(SqlDataReader result = query.ExecuteReader())
while(result.Read()) { Console.WriteLine(result["id"]); }
trans.Commit();
} // no need to close,using will sort that out
}
public static void transaction03(SqlCommand comm) {
using(SqlTransaction trans = comm.Connection.BeginTransaction("Trn03"))
{
comm.Transaction = trans;
using(SqlDataReader result = query.ExecuteReader())
while(result.Read()) { Console.WriteLine(result["id"]); }
trans.Commit();
} // no need to close,using will sort that out
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。