C# MySQL 提交事务不起作用(连接必须有效且打开才能提交事务)
C# MySQL commit transaction is not working (Connection must be valid and open to commit transaction)
我有一个 class 可以帮助我连接到数据库启动事务和 运行 一些查询,一旦我完成所有查询我 commit/rollback 事务.
这是我的class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace RM
{
public class dbConnetionWT
{
//private OdbcConnection conn;
private static readonly string mServer = "localhost";
private static readonly string mDatabase = "pos";
private static readonly string mUid = "root";
private static readonly string mPassword = "";
private static readonly string mPort = "3306";
private string conn_string = String.Format("server={0};user={1};database={2};port={3};password={4};", mServer, mUid, mDatabase, mPort, mPassword);
private MySqlConnection conn;
private MySqlTransaction trans;
public dbConnetionWT()
{
this.conn = new MySqlConnection(this.conn_string);
try
{
this.conn.Open();
}
catch
{
Common.Alert("Database Server is not reachable", "SQL Error");
}
}
public void End()
{
try
{
this.conn.Close();
}
catch
{
Common.Alert("Database Server is not reachable", "SQL Error");
}
}
public void BeginTransaction()
{
this.trans = this.conn.BeginTransaction();
}
public void Commit()
{
try
{
this.trans.Commit();
}
catch(Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
}
}
public void Rollback()
{
try
{
this.trans.Rollback();
}
catch (Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
}
}
// query the data base
public IEnumerable<T> getData<T>(string query, List<MySqlParameter> pars, Func<IDataRecord, T> transform)
{
using (this.conn)
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
yield return transform(rdr);
}
}
}
}
// query the data base
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (this.conn)
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
try
{
object rawValue = cmd.ExecuteScalar();
if (rawValue != null)
{
value = (T)Convert.ChangeType(rawValue, typeof(T));
}
else
{
value = default(T);
}
}
catch (Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
value = default(T);
this.trans.Rollback();
}
}
return value;
}
public bool processQuery(string strSQL, List<MySqlParameter> pars)
{
bool toReturn = true;
using (this.conn)
using (var cmd = new MySqlCommand(strSQL, conn))
{
if (pars != null)
{
foreach (MySqlParameter param in pars)
{
cmd.Parameters.Add(param);
}
}
try
{
cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Common.Alert(ex.ToString(), "SQL Error");
toReturn = false;
this.trans.Rollback();
}
}
return toReturn;
}
}
}
下面是我在遇到问题运行时如何使用它
bool hasSqlError = false;
string sql2 = " INSERT INTO products "
+ " (productUPC, productName, price, createdBy, departmentID, vendorID, productDescription) "
+ " VALUES('123', 'Name', '29.99', '1', '2', '3', 'This is a test')";
if (!dbwt.processQuery(sql2, null))
{
hasSqlError = true;
}
if (!hasSqlError)
{
dbwt.Commit();
dbwt.End();
this.Close();
}
else
{
dbwt.Rollback();
}
但是我在尝试提交时一直收到错误 dbwt.Commit();
这是我得到的异常错误
我该如何解决这个问题?我不明白为什么即使在构造函数中打开连接也会出现此错误?
注意这段代码:
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (this.conn)
如果你在某个时候调用getValue<T>
,一旦块跟随
using (this.conn)
超出范围,this.conn.Dispose()
将被调用,关闭连接。
您在 processQuery()
方法中有一个 using (this.conn)
,using
语句导致您的连接关闭。所以错误信息是有道理的。
您已经在 End()
方法中显式关闭了连接,因此请省略以下行:
using (this.conn)
此外,FWIW,您可能还想确保在回滚后关闭连接:
if (!hasSqlError)
{
dbwt.Commit();
}
else
{
dbwt.Rollback();
}
dbwt.End();
this.Close();
或者,即使在抛出异常的情况下也要确保连接关闭:
try
{
if (!hasSqlError)
{
dbwt.Commit();
}
else
{
dbwt.Rollback();
}
this.Close();
}
finally
{
dbwt.End();
}
我有一个 class 可以帮助我连接到数据库启动事务和 运行 一些查询,一旦我完成所有查询我 commit/rollback 事务.
这是我的class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace RM
{
public class dbConnetionWT
{
//private OdbcConnection conn;
private static readonly string mServer = "localhost";
private static readonly string mDatabase = "pos";
private static readonly string mUid = "root";
private static readonly string mPassword = "";
private static readonly string mPort = "3306";
private string conn_string = String.Format("server={0};user={1};database={2};port={3};password={4};", mServer, mUid, mDatabase, mPort, mPassword);
private MySqlConnection conn;
private MySqlTransaction trans;
public dbConnetionWT()
{
this.conn = new MySqlConnection(this.conn_string);
try
{
this.conn.Open();
}
catch
{
Common.Alert("Database Server is not reachable", "SQL Error");
}
}
public void End()
{
try
{
this.conn.Close();
}
catch
{
Common.Alert("Database Server is not reachable", "SQL Error");
}
}
public void BeginTransaction()
{
this.trans = this.conn.BeginTransaction();
}
public void Commit()
{
try
{
this.trans.Commit();
}
catch(Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
}
}
public void Rollback()
{
try
{
this.trans.Rollback();
}
catch (Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
}
}
// query the data base
public IEnumerable<T> getData<T>(string query, List<MySqlParameter> pars, Func<IDataRecord, T> transform)
{
using (this.conn)
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
yield return transform(rdr);
}
}
}
}
// query the data base
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (this.conn)
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
try
{
object rawValue = cmd.ExecuteScalar();
if (rawValue != null)
{
value = (T)Convert.ChangeType(rawValue, typeof(T));
}
else
{
value = default(T);
}
}
catch (Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
value = default(T);
this.trans.Rollback();
}
}
return value;
}
public bool processQuery(string strSQL, List<MySqlParameter> pars)
{
bool toReturn = true;
using (this.conn)
using (var cmd = new MySqlCommand(strSQL, conn))
{
if (pars != null)
{
foreach (MySqlParameter param in pars)
{
cmd.Parameters.Add(param);
}
}
try
{
cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Common.Alert(ex.ToString(), "SQL Error");
toReturn = false;
this.trans.Rollback();
}
}
return toReturn;
}
}
}
下面是我在遇到问题运行时如何使用它
bool hasSqlError = false;
string sql2 = " INSERT INTO products "
+ " (productUPC, productName, price, createdBy, departmentID, vendorID, productDescription) "
+ " VALUES('123', 'Name', '29.99', '1', '2', '3', 'This is a test')";
if (!dbwt.processQuery(sql2, null))
{
hasSqlError = true;
}
if (!hasSqlError)
{
dbwt.Commit();
dbwt.End();
this.Close();
}
else
{
dbwt.Rollback();
}
但是我在尝试提交时一直收到错误 dbwt.Commit();
这是我得到的异常错误
我该如何解决这个问题?我不明白为什么即使在构造函数中打开连接也会出现此错误?
注意这段代码:
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (this.conn)
如果你在某个时候调用getValue<T>
,一旦块跟随
using (this.conn)
超出范围,this.conn.Dispose()
将被调用,关闭连接。
您在 processQuery()
方法中有一个 using (this.conn)
,using
语句导致您的连接关闭。所以错误信息是有道理的。
您已经在 End()
方法中显式关闭了连接,因此请省略以下行:
using (this.conn)
此外,FWIW,您可能还想确保在回滚后关闭连接:
if (!hasSqlError)
{
dbwt.Commit();
}
else
{
dbwt.Rollback();
}
dbwt.End();
this.Close();
或者,即使在抛出异常的情况下也要确保连接关闭:
try
{
if (!hasSqlError)
{
dbwt.Commit();
}
else
{
dbwt.Rollback();
}
this.Close();
}
finally
{
dbwt.End();
}