zoukankan      html  css  js  c++  java
  • 关于VS2005中自动生成TableAdapter的事务处理

    这里有个方法 http://www.developpez.net/forums/viewtopic.php?p=2527088

    看看源程序大家应该知道怎么做可以解决这个问题了.
    using System; 
    using System.Data; 
    using System.Data.SqlClient; 

    namespace DataAccess.DataSet1TableAdapters 


       partial 
    class Table1TableAdapter 
       

             
    public SqlDataAdapter MyAdapter 
             

                
    get  
                

                   
    return this.Adapter; 
                }
     
             }
     
             }
     

    partial 
    class Table2TableAdapter 



    public SqlDataAdapter MyAdapter 



       
    get 
       

          
    return this.Adapter; 
       }
     

    }
     

    }
     

    }
     

    // Here is the method to implement the Transaction: 

    private void ExecuteTransaction() 



    // The Connection must be identical in order to apply Transaction 

    SqlConnection connection 
    = table1TableAdapter.Connection; 
    table2TableAdapter.Connection 
    = connection; 

    // Start a local Transaction 

    SqlTransaction transaction 
    = connection.BeginTransaction(); 

    table1TableAdapter.MyAdapter.InsertCommand.Transaction 
    = transaction; 

    table2TableAdapter.MyAdapter.InsertCommand.Transaction 
    = transaction; 

    try 


       
    // Update Database 
       table1TableAdapter.Update(dataSet1.Table1); 
       table2TableAdapter.Update(dataSet1.Table2); 
        
       
    // Commit Changes to database 
       transaction.Commit(); 
    }
     

    catch (DataException ex) 



    // Roll back the transaction. 

    if (transaction != null



    transaction.Rollback(); 

    }
     

    Console.WriteLine(
    "Message: {0}", ex.Message); 

    }
     
    finally 


    // Close Conection 
    connection.Close(); 

    }
     

    }
     
    但是,这个写法,还是太傻瓜了,

    在下抛一砖,希望能引玉无数.
  • 相关阅读:
    设计带构造函数的Dog类 代码参考
    动态生成Person类的对象 代码参考
    Fragment传值
    Fragment的创建
    显示Intent和隐式Intent
    Intent及其七大属性及intent-filter设置
    Activity传值的几种方式
    认识Activity
    GridView的基本使用
    Spinner的基本使用
  • 原文地址:https://www.cnblogs.com/sasbya/p/326162.html
Copyright © 2011-2022 走看看