zoukankan      html  css  js  c++  java
  • SqlCeConnectionBeginTransaction 方法

    SqlCeConnectionBeginTransaction 方法

    开始数据库事务。

    命名空间:   System.Data.SqlServerCe
    程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)
    示例

    下面的示例创建一个 SqlCeConnection 和一个 SqlCeTransaction,然后阐释如何使用 BeginTransaction Commit 和 Rollback 方法。

    Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
    conn.Open()
    
    ' Start a local transaction
    '
    Dim tx As SqlCeTransaction = conn.BeginTransaction()
    
    ' By default, commands run in auto-commit mode; 
    '
    Dim cmd1 As SqlCeCommand = conn.CreateCommand()
    
    ' You may create multiple commands on the same connection
    '
    Dim cmd2 As SqlCeCommand = conn.CreateCommand()
    
    ' To enlist a command in a transaction, set the Transaction property
    '
    cmd1.Transaction = tx
    
    Try
        cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')"
        cmd1.ExecuteNonQuery()
    
        ' Auto-commited because cmd2 is not enlisted in a transaction
        '
        cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Decker', 'Barbara')"
        cmd2.ExecuteNonQuery()
    
        ' This will cause referential constraint violation
        '
        cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1"
        cmd1.ExecuteNonQuery()
    
        ' Commit the changes to disk if everything above succeeded
        '
        tx.Commit()
    Catch
        tx.Rollback()
    Finally
        conn.Close()
    End Try
    
    
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'"); 
    conn.Open(); 
     
    // Start a local transaction 
    // 
    SqlCeTransaction tx = conn.BeginTransaction(); 
     
    // By default, commands run in auto-commit mode;  
    // 
    SqlCeCommand cmd1 = conn.CreateCommand(); 
     
    // You may create multiple commands on the same connection 
    // 
    SqlCeCommand cmd2 = conn.CreateCommand(); 
     
    // To enlist a command in a transaction, set the Transaction property 
    // 
    cmd1.Transaction = tx; 
     
    try 

        cmd1.CommandText = "INSERT INTO Shippers ([Company Name]) VALUES ('Northwind Traders')"; 
        cmd1.ExecuteNonQuery(); 
     
        // Auto-commited because cmd2 is not enlisted in a transaction 
        // 
        cmd2.CommandText = "INSERT INTO Employees ([Last Name], [First Name]) VALUES ('Decker', 'Barbara')"; 
        cmd2.ExecuteNonQuery(); 
     
        // This will cause referential constraint violation 
        // 
        cmd1.CommandText = "DELETE FROM Products WHERE [Product ID] = 1"; 
        cmd1.ExecuteNonQuery(); 
     
        // Commit the changes to disk if everything above succeeded 
        // 
        tx.Commit(); 

    catch (Exception) 

        tx.Rollback(); 

    finally 

        conn.Close(); 
  • 相关阅读:
    Asp.net MVC Web.config配置技巧
    MySQL删除表的三种方式
    MySQL中count(字段) ,count(主键 id) ,count(1)和count(*)的区别
    Centos7部署k8s集群及应用
    composer更新指定包||composer 常用命令
    LVS负载均衡策略的部署与应用
    MySQL复制表的三种方式
    Centos7部署Nginx负载均衡Tomcat服务器及session共享架构
    企业级Nginx负载均衡与keepalived高可用实战视频教程
    CentOS下用于查看系统当前登录用户信息的4种方法
  • 原文地址:https://www.cnblogs.com/kittyguo/p/5141374.html
Copyright © 2011-2022 走看看