日期:2014-05-18  浏览次数:20774 次

关于事务
代码如下
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
{
   //关闭数据库
}