我有一个Mysql类来进行这样的查询。
protected MySqlConnection _connection = null;
public bool Connect()
{
try
{
this._connection = new MySqlConnection(this._connectionString);
this._connection.Open();
return true;
}
catch (MySqlException ex)
{
//throw new MySQLException(ex.Message, ex.Number);
return false;
}
}
public MySQLParameters Prepare(MySqlCommand command)
{
try
{
command.Connection = this._connection;
command.CommandType = CommandType.Text;
return new MySQLParameters(command);
}
catch (MySqlException ex)
{
throw new MySQLException(ex.Message, ex.Number);
}
}
public void Query(MySqlCommand command)
{
try
{
command.Connection = this._connection;
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
catch (MySqlException ex)
{
throw new MySQLException(ex.Message, ex.Number);
}
}
public DataSet QueryResult(MySqlCommand command)
{
try
{
DataSet dataSet = new DataSet();
command.Connection = this._connection;
command.CommandType = CommandType.Text;
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
dataAdapter.Fill(dataSet);
return dataSet;
}
catch (MySqlException ex)
{
throw new MySQLException(ex.Message, ex.Number);
}
}
现在我想在事务
中使用它但我不知道在哪里添加transaction commit
和rollback
因为现在我像这样使用我的班级。
RemoteMySQL mySql = new RemoteMySQL();
mySql.Connect();
string sql = "INSERT INTO `table1` SET ";
sql += "`name` = @name, ";
sql += "`lastname` = @lastname;";
MySQLParameters parameters = mySql.Prepare(sql);
parameters["name"] = name;
parameters["lastname"] = lastname;
mySql.Query(parameters.GetCommand());
sql = "SELECT LAST_INSERT_ID();";
ds = mySql.QueryResult(sql);
customerId = int.Parse(ds.Tables[0].Rows[0][0].ToString());
sql = "INSERT INTO `table2` SET ";
sql += "`customer_id` = @customer_id, ";
sql += "`item` = @item, ";
sql += "`detail` = @detail;";
parameters = mySql.Prepare(sql);
parameters["customer_id"] = customerId;
parameters["item"] = item;
parameters["detail"] = detail;
mySql.Query(parameters.GetCommand());
// I want to commit here
// If can't insert to table2 will rollback table1 too.
我看一下http://zetcode.com/db/mysqlcsharptutorial/(关于trasaction)
但仍然不知道如何使用我的代码(在哪里放置提交和回滚)
感谢您的帮助。
答案 0 :(得分:0)
应该以与SQLConnection相同的方式使用
public void Query(MySqlCommand command)
{
var transaction = this._connection.BeginTransaction("SampleTransaction");
try
{
command.Connection = this._connection;
command.CommandType = CommandType.Text;
command.Transaction = transaction;
command.ExecuteNonQuery();
transaction.Commit();
}
catch (MySqlException ex)
{
transaction.Rollback();
throw new MySQLException(ex.Message, ex.Number);
}
}
答案 1 :(得分:0)
我想我已经有了解决方案,但不知道这是正确的做法吗?
我为像@ragerory
这样的交易事件创建了新方法。
public void TransactionQuery(ArrayList list)
{
try
{
foreach (MySqlCommand command in list)
{
//Console.WriteLine(command.CommandText);
command.Connection = this._connection;
command.CommandType = CommandType.Text;
command.Transaction = this._transaction;
command.ExecuteNonQuery();
}
this._transaction.Commit();
}
catch (MySqlException ex)
{
this._transaction.Rollback();
throw new MySQLException(ex.Message, ex.Number);
}
}
我喜欢这样。
RemoteMySQL mySql = new RemoteMySQL();
mySql.Connect();
string sql = "INSERT INTO `table1` SET ";
sql += "`name` = @name, ";
sql += "`lastname` = @lastname;";
MySQLParameters parameters = mySql.Prepare(sql);
parameters["name"] = name;
parameters["lastname"] = lastname;
//mySql.Query(parameters.GetCommand()); not use this.
list.Add(parameters.GetCommand());
sql = "SELECT LAST_INSERT_ID();";
ds = mySql.QueryResult(sql);
customerId = int.Parse(ds.Tables[0].Rows[0][0].ToString());
sql = "INSERT INTO `table2` SET ";
sql += "`customer_id` = @customer_id, ";
sql += "`item` = @item, ";
sql += "`detail` = @detail;";
parameters = mySql.Prepare(sql);
parameters["customer_id"] = customerId;
parameters["item"] = item;
parameters["detail"] = detail;
//mySql.Query(parameters.GetCommand()); not use this.
list.Add(parameters.GetCommand());
// And call this method last line.
mySql.TransactionQuery(list);
现在有效。但问题是。可以这样做吗? (对吗?)
谢谢。