你的事物写得不对。
string sql = "update ...";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DSN"].ToString());
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.Transaction = sqlTran;
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sql = "update ...";
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sqlTran.Commit();
}
catch(Exception e1)
{
try
{
sqlTran.Rollback();
}
catch(SqlException ex)
{
throw ex;
}
throw e1;
}
finally
{
conn.Close();
}
string sql = "update ...";
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DSN"].ToString());
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.Transaction = sqlTran;
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sql = "update ...";
cmd.CommandText = sql;
cmd.ExecuteNonQuery(); sqlTran.Commit();
}
catch(Exception e1)
{
try
{
sqlTran.Rollback();
}
catch(SqlException ex)
{
throw ex;
}
throw e1;
}
finally
{
conn.Close();
}
请参考http://community.csdn.net/Expert/topic/2995/2995537.xml?temp=.2118952
楼主的问题AddOrganizeKPIListCmd()这是增加操作方法,为什么增加有两次记录了,在没有记录的情况下,可以用修改方法来操作,想了两天了,还没有解决,请指教
---什么意思?
myConnection.Open();// Start a local transaction.
SqlTransaction myTrans = myConnection.BeginTransaction();// Enlist the command in the current transaction.
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.Transaction = myTrans;try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
} Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
myConnection.Open();// Start a local transaction.
SqlTransaction myTrans = myConnection.BeginTransaction();// Enlist the command in the current transaction.
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.Transaction = myTrans;try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
} Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
俺解决了,你将两个Sql语句写在一起,一并提交,这样就可以了。如下:
public bool DelCourse(int pID)
{
wisdeconnection conn=new wisdeconnection();
try
{
conn.BeginTransaction();
conn.RunSql("delete from courseware where id= "+pID
+"delete from coursepop where courseid="+pID);
conn.CommitTransaction();
return true;
}
catch(Exception er)
{
conn.Rollback();
throw new Exception(er.Message);
}
finally
{
conn.Dispose();
}
}