问题描述
System.InvalidOperationException:Theconnectionisnotopen.在MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLeveliso)在MySql.Data.MySqlClient.MySqlConnection.BeginTransaction()[WebMethod]publicstringInsertData(stringnewidtitlecontent){try{stringresult="";stringsql="insertintot1(newidtitle)values('"+newid+"','"+title+"')";stringsql1="insertintot2(newidcontent)values('"+newid+"','"+content+"')";MySqlConnectionDefSqlCon=newMySqlConnection("server=192.168.0.1;userid=net;password=123;database=test");MySqlTransactiontran=DefSqlCon.BeginTransaction();DefSqlCon.Open();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=tran;sqlcmd.Connection=DefSqlCon;sqlcmd.ExecuteNonQuery();MySqlCommandsqlcmd1=newMySqlCommand(sql1,DefSqlCon);sqlcmd1.Transaction=tran;sqlcmd1.Connection=DefSqlCon;sqlcmd1.ExecuteNonQuery();tran.Commit();result="1";returnresult;}catch(Exceptionerr){returnerr.ToString();}finally{}
解决方案
解决方案二:
没人能帮一下吗
解决方案三:
MySqlTransactiontran=DefSqlCon.BeginTransaction();DefSqlCon.Open();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=tran;改称DefSqlCon.Open();MySqlTransactiontran=DefSqlCon.BeginTransaction();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=tran;
解决方案四:
MySqlTransactiontran=DefSqlCon.BeginTransaction();应该是这句,你没有开始事务,然后在后面用tran.Commit();是不可以的!
解决方案五:
引用2楼xiaoyuzi的回复:
MySqlTransactiontran=DefSqlCon.BeginTransaction();DefSqlCon.Open();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=tran;改称DefSqlCon.Open();MySqlTransactiontran=DefSqlCon.BeginTransaction();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=…
还是一样!
解决方案六:
OK
解决方案七:
引用3楼phper2008的回复:
MySqlTransactiontran=DefSqlCon.BeginTransaction();应该是这句,你没有开始事务,然后在后面用tran.Commit();是不可以的!
报错是这句有问题,可是怎么改呢?
解决方案八:
MySql.Data.MySqlClient.MySqlException:Unknowncolumn'newid'in'fieldlist'在MySql.Data.MySqlClient.MySqlStream.OpenPacket()[WebMethod]publicstringInsertData(stringnewidtitlecontent){try{stringresult="";stringsql="insertintot1(newidtitle)values('"+newid+"','"+title+"')";stringsql1="insertintot2(newidcontent)values('"+newid+"','"+content+"')";MySqlConnectionDefSqlCon=newMySqlConnection("server=192.168.0.1;userid=net;password=123;database=test");DefSqlCon.Open();//放在这就可以了,呵呵,是不是不能同时插入两个表啊?提示错误MySqlTransactiontran=DefSqlCon.BeginTransaction();MySqlCommandsqlcmd=newMySqlCommand(sql,DefSqlCon);sqlcmd.Transaction=tran;sqlcmd.Connection=DefSqlCon;sqlcmd.ExecuteNonQuery();MySqlCommandsqlcmd1=newMySqlCommand(sql1,DefSqlCon);sqlcmd1.Transaction=tran;sqlcmd1.Connection=DefSqlCon;sqlcmd1.ExecuteNonQuery();tran.Commit();result="1";returnresult;