• 关于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(); 

    }
     

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

    在下抛一砖,希望能引玉无数.
  • 相关阅读:
    js技巧收集(200多个)
    我的博客开张了,欢迎大家前来做客
    认识ASP.NET配置文件Web.config
    C#纯数学方法递归实现货币数字转换中文
    Asp.Net细节性问题技巧精萃(转载)
    ASP.NET2.0+SQL Server2005构建多层应用
    ASP.NET页面间的传值的几种方法(转载)
    介绍几种 ADO.net 中的数据库连接方式
    hdu 1401
    poj 1338 丑数
  • 原文地址:https://www.cnblogs.com/sasbya/p/326162.html
Copyright © 2020-2023  润新知