执行下面代码会有超时异常?

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["textconnstring"].ToString()))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlTransaction trans
= conn.BeginTransaction(IsolationLevel.ReadUncommitted);

try
{

SqlCommand cmd
= new SqlCommand();
cmd.Connection
= conn;
cmd.CommandText
= sqlStr1;
cmd.Transaction
= trans;
cmd.ExecuteNonQuery();
cmd.CommandText
= sqlStr2;
cmd.Transaction
= trans;
cmd.ExecuteNonQuery();

using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["textconnstring"].ToString()))
{
SqlCommand cmd1
= new SqlCommand(sqlStr, conn1);
SqlDataAdapter sda
= new SqlDataAdapter(cmd1);
DataSet ds
=new DataSet();
sda.Fill(ds);
}
trans.Commit();

}
catch(SqlException ex)
{
trans.Rollback();
Console.WriteLine(ex.Message);
}
}
其中sqlStr1和 sqlStr2为插入语句,sqlStr为查询语句。conn和conn2是两个不同的连接,但是在执行时到了“sda.Fill(ds)”时程序会卡住,然后就是超时错误了,不知是什么原因?
牛魔王的故事
浏览 459回答 4
4回答

红颜莎娜

你的把 conn1.Close() 放到 trans.Commit(); 的后面.

翻阅古今

cmd1.CommandTimeout=0就好了
打开App,查看更多内容
随时随地看视频慕课网APP