本文介绍了使用代码的Sql事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何在下面的代码中使用交易代码进行多次查询 私有 SqlConnection DBCon { get { return new SqlConnection(ConfigurationManager.ConnectionStrings [ ]。ConnectionString ); } } 受保护 DataTable ExecuteQry( string Qry) { DataTable dt = new DataTable(); 尝试 { 使用(SqlConnection myCon = DBCon) { SqlCommand myCommand = new SqlCommand(Qry,myCon); myCommand.CommandType = CommandType.Text; // 设置参数 foreach (SqlParameter参数 in 参数) { myCommand.Parameters.AddWithValue(Parameter.ParameterName,Parameter.Value); } // 执行命令 SqlDataAdapter da = new SqlDataAdapter(myCommand); // 执行命令 myCon.Open(); da.Fill(dt); myCon.Close(); } } catch (例外情况) { ex.ToString( ); } return dt; } 我的尝试: i已尝试给定代码 受保护 boolExecuteQry( string Qry, string Qry1) { 尝试 { 使用(SqlConnection myCon = DBCon) { myCon.Open(); 使用(SqlTransaction tr = myCon.BeginTransaction()) { try { if (Qry!= ) { SqlCommand myCommand = new SqlCommand(Qry,myCon,tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter参数 in 参数) { myCommand。 Parameters.AddWithValue(Parameter.ParameterName,Parameter.Value); } myCommand.ExecuteNonQuery(); } if (Qry1!= ) { SqlCommand myCommand = new SqlCommand(Qry1,myCon,tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter参数 in 参数) { myCommand。 Parameters.AddWithValue(Parameter.ParameterName,Parameter.Value); } myCommand.ExecuteNonQuery(); } } } catch (例外情况) { ex.ToString(); } } 解决方案 我甚至不去问这个代码用于什么。无论你想要什么目的,它看起来都是错误的。 1)你有两个Qry变量,它们都没有正确命名来描述它们的用途。 2)你可以看似执行BOTH查询,他们都会使用同一组参数对象。 3)你的参数是来自同类中的其他地方而不是通过方法参数传入。 4)您的参数变量也没有正确命名,因为它没有描述参数对象的集合。 5)你的外部try / catch块,没有阻挡块! 但是,关于手头的问题。成功完成查询后,您从未提交过该事务。 哦,你错过了代码底部附近的一些大括号。 修改后的代码应该更像这样: protected boolExecuteQry( string Qry, string Qry1) { 尝试 { 使用(SqlConnection myCon = DBCon) { myCon.Open(); SqlTransaction tr = myCon.BeginTransaction(); 尝试 { 如果(Qry! = ) { SqlCommand myCommand = new SqlCommand(Qry,myCon,tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter参数 in 参数) { myCommand。 Parameters.AddWithValue(Parameter.ParameterName,Parameter.Value); } myCommand.ExecuteNonQuery(); } if (Qry1!= ) { SqlCommand myCommand = new SqlCommand(Qry1,myCon,tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter参数 in 参数) { myCommand。 Parameters.AddWithValue(Parameter.ParameterName,Parameter.Value); } myCommand.ExecuteNonQuery(); } tr.Commit(); } catch (例外情况) { // 某些内容失败,因此回滚事务。 tr.Rollback(); // 这行代码绝对没有!为什么会在这里? ex.ToString(); } } } catch (例外情况) { // 如果连接无法打开,你在这做什么? } } how can i use transaction code in below code for multiple queryprivate SqlConnection DBCon { get { return new SqlConnection(ConfigurationManager.ConnectionStrings[" "].ConnectionString); } } protected DataTable ExecuteQry(string Qry) { DataTable dt = new DataTable(); try { using (SqlConnection myCon = DBCon) { SqlCommand myCommand = new SqlCommand(Qry, myCon); myCommand.CommandType = CommandType.Text; //Set Parameters foreach (SqlParameter Parameter in parameter) { myCommand.Parameters.AddWithValue(Parameter.ParameterName, Parameter.Value); } //Execute the command SqlDataAdapter da = new SqlDataAdapter(myCommand); // Execute the command myCon.Open(); da.Fill(dt); myCon.Close(); } } catch(Exception ex) { ex.ToString(); } return dt; }What I have tried:i have tried the given codeprotected boolExecuteQry(string Qry,string Qry1) { try { using (SqlConnection myCon = DBCon) { myCon.Open(); using (SqlTransaction tr = myCon.BeginTransaction()) { try { if (Qry != "") { SqlCommand myCommand = new SqlCommand(Qry, myCon, tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter Parameter in parameter) { myCommand.Parameters.AddWithValue(Parameter.ParameterName, Parameter.Value); } myCommand.ExecuteNonQuery(); }if (Qry1 != "") { SqlCommand myCommand = new SqlCommand(Qry1, myCon, tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter Parameter in parameter) { myCommand.Parameters.AddWithValue(Parameter.ParameterName, Parameter.Value); } myCommand.ExecuteNonQuery(); } } } catch(Exception ex) { ex.ToString(); } } 解决方案 I'm, not even going to ask what this code is being used for. It looks wrong for whatever purpose you intend.1) You've got two Qry variables, neither of which are properly named to describe what they are used for.2) You can seemingly execute BOTH queries, of which they will both use the same set of parameter objects.3) Your parameters are coming from somewhere else in the class and not from being passed in via the method arguments.4) Your "parameter" variable is also not properly named as it's not describing a collection of parameter objects.5) Your outside try/catch block, well, has no catch block!But, on to the question at hand. You never committed the transaction upon successful completion of the query.Oh, and you're missing a few closing curly braces near the bottom of the code.Your modified code should look more like this:protected boolExecuteQry(string Qry,string Qry1){ try { using (SqlConnection myCon = DBCon) { myCon.Open(); SqlTransaction tr = myCon.BeginTransaction(); try { if (Qry != "") { SqlCommand myCommand = new SqlCommand(Qry, myCon, tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter Parameter in parameter) { myCommand.Parameters.AddWithValue(Parameter.ParameterName, Parameter.Value); } myCommand.ExecuteNonQuery(); } if (Qry1 != "") { SqlCommand myCommand = new SqlCommand(Qry1, myCon, tr); myCommand.CommandType = CommandType.Text; foreach (SqlParameter Parameter in parameter) { myCommand.Parameters.AddWithValue(Parameter.ParameterName, Parameter.Value); } myCommand.ExecuteNonQuery(); } tr.Commit(); } catch(Exception ex) { // Something failed so rollback the transaction. tr.Rollback(); // This line of code does absolutely NOTHING! Why is it here? ex.ToString(); } } } catch (Exception ex) { // WHat do you do here if the connection fails to open? }} 这篇关于使用代码的Sql事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 18:23