首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于事务,该怎么处理

2012-05-27 
关于事务代码如下C# codemyConnection.open()SqlCommand myCommand myConnection.CreateCommand()SqlTr

关于事务
代码如下

C# code
       myConnection.open()        SqlCommand myCommand = myConnection.CreateCommand();        SqlTransaction myTrans;        myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);                                myCommand.Connection = myConnection;        myCommand.Transaction = myTrans;        try        {                       myCommand.CommandType = CommandType.StoredProcedure;           myCommand.CommandText = "A";           myCommand.ExecuteNonQuery();                       myCommand.CommandType = CommandType.Text;            myCommand.CommandText = "select str from B where number='111'";            string temp = null;            SqlDataReader rd = myCommand.ExecuteReader();            if (rd.Read())            {                temp = rd.GetString(0).ToString();                                          }              myCommand.CommandType = CommandType.StoredProcedure;            myCommand.CommandText = "C";            SqlParameter spam1 = new SqlParameter();                spam1.ParameterName = "@code";                spam1.SqlDbType = SqlDbType.VarChar;                   spam1.Size = 25;                spam1.Value = temp;                spam1.Direction = ParameterDirection.Input;                myCommand.Parameters.Add(spam1);             myCommand.ExecuteNonQuery();               myTrans.Commit();        }        cacth(Exception)        {             myTrans.Rollback();        }


以上代买该怎么修正 谢谢!
异常提示:System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

[解决办法]
try{}
catch()
finally
{
 关闭数据连接

}
[解决办法]
C# code
myConnection.open()        SqlCommand myCommand = myConnection.CreateCommand();        SqlTransaction myTrans;        myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);                                myCommand.Connection = myConnection;        myCommand.Transaction = myTrans;        try        {                       myCommand.CommandType = CommandType.StoredProcedure;           myCommand.CommandText = "A";           myCommand.ExecuteNonQuery();                       myCommand.CommandType = CommandType.Text;            myCommand.CommandText = "select str from B where number='111'";            string temp = null;            SqlDataReader rd = myCommand.ExecuteReader();            if (rd.Read())            {                temp = rd[0].ToString();//如果为null不会报错                              temp = rd.GetString(0);//如果为null会报错            }              myCommand.CommandType = CommandType.StoredProcedure;            myCommand.CommandText = "C";            SqlParameter spam1 = new SqlParameter();                spam1.ParameterName = "@code";                spam1.SqlDbType = SqlDbType.VarChar;                   spam1.Size = 25;                spam1.Value = temp;                spam1.Direction = ParameterDirection.Input;                myCommand.Parameters.Add(spam1);             myCommand.ExecuteNonQuery();               myTrans.Commit();        }        cacth(Exception)        {             myTrans.Rollback();        }finally{   //关闭数据库} 

热点排行