本文整理汇总了C#中MySql.Data.MySqlClient.MySqlConnection.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# MySqlConnection.BeginTransaction方法的具体用法?C# MySqlConnection.BeginTransaction怎么用?C# MySqlConnection.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MySql.Data.MySqlClient.MySqlConnection
的用法示例。
在下文中一共展示了MySqlConnection.BeginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EditSpecification
public void EditSpecification(Specification s)
{
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "EditPackageSpecification";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("SpecificationID", s.ID);
cmd.Parameters.AddWithValue("PWeight", s.Weight);
cmd.Parameters.AddWithValue("DeimensionHeight", s.Height);
cmd.Parameters.AddWithValue("DimensionWidth", s.Width);
cmd.Parameters.AddWithValue("DimensionLength", s.Length);
cmd.ExecuteNonQuery();
connect.Close();
}
catch (InvalidOperationException ioException)
{
connect.Close();
}
}
}
}
示例2: CreateBank
public int CreateBank(Bank b)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewBank";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("SortCode", b.SortCode);
cmd.Parameters.AddWithValue("AccountNumber", b.ID);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例3: executeBatch
/// <summary>
/// Execute several commands on the same transaction
/// </summary>
/// <param name="commands"></param>
/// <returns></returns>
public Result executeBatch(List<MySqlCommand> commands)
{
Result result = new Result(true);
using (MySqlConnection cn = new MySqlConnection(DefaultConfig.ConnectionString))
{
cn.Open();
using (MySqlTransaction trans = cn.BeginTransaction())
{
try
{
foreach (MySqlCommand cmd in commands)
{
cmd.Transaction = trans;
cmd.Connection = cn;
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Log.error(ex, "Batch");
result.ErrorDetails = ex;
result.Success = false;
}
}
}
return result;
}
示例4: ExecuteSqlTran
public static int ExecuteSqlTran(string[] strSQLS)
{
using (MySqlConnection conn = new MySqlConnection(connString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
MySqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < strSQLS.Length; n++)
{
string strsql = strSQLS[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
throw;
}
finally
{
conn.Close();
}
}
}
示例5: CreateUser
public int CreateUser(User u)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewUserAccount";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("PUsername", u.username);
cmd.Parameters.AddWithValue("PPWD", u.password);
cmd.Parameters.AddWithValue("EmployeeID", u.EmployeeID);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例6: SettingsSetPassword
public static bool SettingsSetPassword(string password)
{
using (MySqlConnection mySqlCon = new MySqlConnection(LiveDocs.backend.database.Database.ConnectionString))
{
mySqlCon.Open();
using (MySqlTransaction mySqlTransaction = mySqlCon.BeginTransaction())
{
using (MySqlCommand mySqlCommand = mySqlCon.CreateCommand())
{
mySqlCommand.Transaction = mySqlTransaction;
try
{
mySqlCommand.Parameters.AddWithValue("@password", password);
mySqlCommand.Parameters.AddWithValue("@username", HttpContext.Current.Session["username"]);
mySqlCommand.Parameters.AddWithValue("@token", HttpContext.Current.Session["token"]);
mySqlCommand.CommandText =
"UPDATE Users SET Password=Password(@password) WHERE [email protected] AND [email protected]";
mySqlCommand.ExecuteNonQuery();
mySqlTransaction.Commit();
}
catch (Exception)
{
mySqlTransaction.Rollback();
return false;
}
}
}
}
return true;
}
示例7: ManipulandoDadosNaBase
private bool ManipulandoDadosNaBase(StringBuilder querySql)
{
// retorna true em caso de sucesso.
bool executou = false;
MySqlTransaction transacaoSql = null;
MySqlConnection conexaoSql = null;
MySqlCommand comandoSql = null;
try
{
// Abrindo conexão
conexaoSql = new MySqlConnection(GetConfiguracoes());
conexaoSql.Open();
// recuperando instancia de MySqlCommand
comandoSql = conexaoSql.CreateCommand();
// Iniciando transacao
transacaoSql = conexaoSql.BeginTransaction();
// Definindo conexao e transacao para MySqlCommand
comandoSql.Connection = conexaoSql;
comandoSql.Transaction = transacaoSql;
// Definindo query para comandoSql
comandoSql.CommandText = querySql.ToString();
// executando query
executou = comandoSql.ExecuteNonQuery() > 0;
// integridade referencial, confirmando execucao.
transacaoSql.Commit();
}
catch (Exception ex)
{
try
{
// integridade referencial, desfazendo execucao.
transacaoSql.Rollback();
}
catch (MySqlException e)
{
if (transacaoSql.Connection != null)
throw new Exception("Uma exceção do tipo " + e.GetType() + " foi encontrado ao tentar desfazer a transação.");
}
throw new Exception("Uma exceção do tipo " + ex.GetType() + " foi encontrado enquanto a query estava sendo processada.");
}
finally
{
if (conexaoSql != null)
conexaoSql.Close();
if (comandoSql != null)
comandoSql.Dispose();
if (transacaoSql != null)
transacaoSql.Dispose();
}
return executou;
}
示例8: LoadFileToDb
public static void LoadFileToDb(string connStr, string tableName, string filePath)
{
if (filePath == null || !File.Exists(filePath))
{
return;
}
var conn = new MySqlConnection(connStr);
var bl = new MySqlBulkLoader(conn)
{
TableName = tableName, FieldTerminator = ",", LineTerminator = "\r\n", FileName = filePath
};
conn.Open();
MySqlTransaction trans = conn.BeginTransaction();
try
{
// Upload data from file
int count = bl.Load();
trans.Commit();
conn.Close();
}
catch (Exception ex)
{
trans.Rollback();
}
}
示例9: CreateClientUser
public int CreateClientUser(ClientUser user)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewClientUser";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("AccountID", user.AccountID);
cmd.Parameters.AddWithValue("pName", user.Name);
cmd.Parameters.AddWithValue("pUsername", user.AccountID);
cmd.Parameters.AddWithValue("pPwd", user.Name);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例10: CreatePackage
public int CreatePackage(Package p)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewPackage";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("SpecificationID", p.SpecificationID);
cmd.Parameters.AddWithValue("GoodsID", p.GoodsID);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例11: CreateDepot
public int CreateDepot(Depot d)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewRole";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("DepotName", d.DepotName);
cmd.Parameters.AddWithValue("FloorSpace", d.FloorSpace);
cmd.Parameters.AddWithValue("NumOfVehicles", d.NumVehicles);
cmd.Parameters.AddWithValue("AddressID", d.Address);
cmd.Parameters.AddWithValue("DepotManager", d.ManagerID);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例12: CreateAccountType
public int CreateAccountType(Account_Type a)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewAccountType";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("AccountName", a.Name);
cmd.Parameters.AddWithValue("PBenifit", a.Benefit);
cmd.Parameters.AddWithValue("PCost", a.Cost);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}
示例13: ExecuteSql
/// <summary>
/// 执行SQL语句,使用事务
/// </summary>
/// <param name="SQLString">SQL语句</param>
public static int ExecuteSql(string SQLString)
{
// SQLString = SQLString.Replace("'", "''").Replace(@"\",@"\\");
int result = -1;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
MySqlTransaction tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
cmd.CommandText = SQLString;
result = cmd.ExecuteNonQuery();
tx.Commit();
}
catch (MySql.Data.MySqlClient.MySqlException E)
{
result = -1;
tx.Rollback();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
return result;
}
}
示例14: ExecuteCommand
//带参数的执行命令
public static int ExecuteCommand(MySqlTransaction mySqlTransaction,string sql, params MySqlParameter[] values)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
mySqlTransaction = connection.BeginTransaction();
MySqlCommand cmd = new MySqlCommand(sql, connection, mySqlTransaction);
cmd.Parameters.AddRange(values);
cmd.ExecuteNonQuery(); // ExecuteNonQuery()一般用于UPDATE、INSERT或DELETE语句,其中唯一的返回值是受影响的记录个数
mySqlTransaction.Commit();
return 1;
}
catch
{
mySqlTransaction.Rollback();
return 0;
}
finally
{
connection.Close();
}
}
}
示例15: CreateMessage
public int CreateMessage(Message message)
{
int ret = 0;
using (connect = new MySqlConnection(_connectionString))
{
connect.Open();
using (MySqlTransaction transaction = connect.BeginTransaction())
{
try
{
string query = "NewMessage";
var cmd = new MySqlCommand(query, connect) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("mText", message.MessageBody);
cmd.Parameters.AddWithValue("mEmail", message.Email);
cmd.Parameters.AddWithValue("mName", message.Name);
ret = int.Parse(cmd.ExecuteScalar().ToString());
transaction.Commit();
connect.Close();
}
catch (InvalidOperationException ioException)
{
transaction.Rollback();
connect.Close();
}
}
}
return ret;
}