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();
}
}
}
}
作者:过错
出处:http://www.cnblogs.com/wang2650/
关于作者:net开发做的久而已。十余年时光虚度!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com
联系我,非常感谢。

浙公网安备 33010602011771号