日期:2014-05-18 浏览次数:20774 次
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(); }
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 { //关闭数据库 }