subsonic事务

 

ContractedBlock.gif ExpandedBlockStart.gif Code
private void button1_Click(object sender, EventArgs e)
        {
            QueryCommandCollection qcc 
= new QueryCommandCollection(CsdKlszMigrateVehicleSerialDef.VehicleSeriesIdColumn.ColumnName);
            CsdKlszMigrateVehicleSerialDef seri 
= new CsdKlszMigrateVehicleSerialDef();
            seri.VehicleSeriesCode 
= "0008";
            seri.VehicleSeriesName 
= "oooo";
            seri.IsEnable 
= true;
            seri.StatusCode 
= "--";
            qcc.Add(seri.GetSaveCommand());

            
//获得sql语句
           string s= seri.GetSaveCommand().CommandSql;

            CsdKlszGroupStruc str 
= new CsdKlszGroupStruc();
            str.GroupCode 
= "0003";
            str.IsEnable 
= true;
            str.VehicleSeriesId 
= seri.VehicleSeriesId;
            qcc.Add(str.GetSaveCommand());
            DataService.ExecuteTransaction(qcc, DataService.Provider.Name);

            
//---------------------

            
using (AutomaticConnectionScope com = new AutomaticConnectionScope(DataService.Provider))
            {
                
string sql1 = @"/* ExecuteTransaction() */ /* GetInsertSql(csd_klsz_migrate_vehicle_serial_def) */ INSERT INTO [dbo].[csd_klsz_migrate_vehicle_serial_def]([vehicle_series_code],[vehicle_series_name],[vehicle_series_desc],[is_enable],[status_code],[remark],[modify_userid],[modify_time],[create_userid],[create_time]) VALUES('1','1','1',1,'1','1','1',getdate(),'1',getdate());SELECT SCOPE_IDENTITY() AS newID;";
                
if (com.Connection.State == ConnectionState.Closed)
                    com.Connection.Open();

                SqlTransaction trans 
= (SqlTransaction)com.Connection.BeginTransaction();

                SqlCommand cmd 
= new SqlCommand(sql1, (SqlConnection)com.Connection, trans);

                
object o = cmd.ExecuteScalar();
                
int t = Convert.ToInt16(o);
                sql1 
= @" /* ExecuteTransaction() */ /* GetInsertSql(csd_klsz_group_struc) */ INSERT INTO [dbo].[csd_klsz_group_struc]([group_code],[seq_no],[qty],[parent_id],[level],[is_enable],[ref_group_id],[choic_mod_id],[remark],[vehicle_series_id],[modify_userid],[modify_time],[create_userid],[create_time]) VALUES('1','1',1,1,1,1,1,1,'@remark'," + t + ",'@remark',getdate(),'@remark',getdate());SELECT SCOPE_IDENTITY() AS newID;";
                cmd 
= new SqlCommand(sql1, (SqlConnection)com.Connection, trans);
                cmd.ExecuteScalar();

                trans.Commit();
            }
        }

 

ContractedBlock.gif ExpandedBlockStart.gif Code
public override void ExecuteTransaction(QueryCommandCollection commands)
        {
            
//make sure we have at least one
            if(commands.Count == 0)
                
throw new ArgumentOutOfRangeException("commands""No commands present");

            
//a using statement will make sure we close off the connection
            using(AutomaticConnectionScope conn = new AutomaticConnectionScope(this))
            {
                
                
//open up the connection and start the transaction
                if(conn.Connection.State == ConnectionState.Closed)
                    conn.Connection.Open();

                SqlTransaction trans 
= (SqlTransaction)conn.Connection.BeginTransaction();

                SqlCommand cmd;
                
foreach(QueryCommand qry in commands)
                {
                    
if(qry.CommandType == CommandType.Text)
                        qry.CommandSql 
= String.Concat("/* ExecuteTransaction() */ ", qry.CommandSql);
                    cmd 
= new SqlCommand(qry.CommandSql, (SqlConnection)conn.Connection, trans);
                    cmd.CommandType 
= qry.CommandType;

                    
if (commands.IsMasterDetail  && qry.Parameters.Contains("@" + commands.RelationFieldName))
                    {
                        qry.Parameters.GetParameter(
"@" + commands.RelationFieldName).ParameterValue = commands.RelationFieldValue;
                    }

                    AddParams(cmd, qry);

                  
                    
try
                    {
                        
if (commands.IsMasterDetail && commands.RelationFieldValue == null)
                            {
                                
object retValue = cmd.ExecuteScalar();

                                commands.RelationFieldValue 
= retValue;
                            }
                            
else
                            {
                                cmd.ExecuteNonQuery();
                            }
                    }
                    
catch(Exception ex)
                    {
                    
//catch(SqlException)
                    
//{
                        
//if there'commands.RelationFieldName an error, roll everything back
                        trans.Rollback();

                        
//throw the error retaining the stack.
                        throw;
                    }
                }
                
//if we get to this point, we're good to go
                trans.Commit();
            }
        }

转载于:https://www.cnblogs.com/joe-yang/archive/2009/05/26/1490460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值