我有一个类可以帮助我连接到数据库启动事务并运行一些查询,一旦我完成所有查询,我提交/回滚事务。
这是我的班级
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();
这是我得到的异常错误
如何更正此问题?即使我在构造函数中打开连接,我也不明白为什么会出现此错误?
答案 0 :(得分:1)
请注意以下代码:
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (this.conn)
如果您在某个时刻致电getValue<T>
,请执行
using (this.conn)
超出范围,将调用this.conn.Dispose()
,关闭连接。
答案 1 :(得分:0)
using (this.conn)
方法中有processQuery()
,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();
}