CUBRID学习笔记 33 net事务 cubrid教程示例

conn.BeginTransaction();

string sql = "create table t(idx integer)";
using (CUBRIDCommand command = new CUBRIDCommand(sql, conn))
{
    command.ExecuteNonQuery();
}

conn.Rollback();

conn.BeginTransaction();

sql = "create table t(idx integer, a varchar(20))";
using (CUBRIDCommand command = new CUBRIDCommand(sql, conn))
{
    command.ExecuteNonQuery();
}

conn.Commit();



//Here is a complete example: using CUBRID.Data.CUBRIDClient; using System.Diagnostics; namespace TransactionExample { class Program { private static void ExecuteSQL(string sql, CUBRIDConnection conn) { using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn)) { cmd.ExecuteNonQuery(); } } private static int GetTablesCount(string tableName, CUBRIDConnection conn) { int count = 0; string sql = "select count(*) from db_class where class_name = '" + tableName + "'"; using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn)) { count = (int)cmd.ExecuteScalar(); } return count; } static void Main(string[] args) { CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder("localhost", "demodb", "public", "", "33000"); using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString())) { conn.Open(); ExecuteSQL("drop table if exists t", conn); conn.BeginTransaction(); string sql = "create table t(idx integer)"; using (CUBRIDCommand command = new CUBRIDCommand(sql, conn)) { command.ExecuteNonQuery(); } int tablesCount = GetTablesCount("t", conn); Debug.Assert(tablesCount == 1); conn.Rollback(); //Verify the table does not exist tablesCount = GetTablesCount("t", conn); Debug.Assert(tablesCount == 0); conn.BeginTransaction(); sql = "create table t(idx integer)"; using (CUBRIDCommand command = new CUBRIDCommand(sql, conn)) { command.ExecuteNonQuery(); } tablesCount = GetTablesCount("t", conn); Debug.Assert(tablesCount == 1); conn.Commit(); tablesCount = GetTablesCount("t", conn); Debug.Assert(tablesCount == 1); conn.BeginTransaction(); ExecuteSQL("drop table t", conn); conn.Commit(); tablesCount = GetTablesCount("t", conn); Debug.Assert(tablesCount == 0); conn.Close(); } } } }

  

原文地址:https://www.cnblogs.com/wang2650/p/5287945.html