ActiveRecord 切换数据库保存数据(二)

本文介绍了一种使用ActiveRecord框架进行数据库更新的方法,特别是针对设置了关联的数据表,并通过具体代码示例展示了如何实现批量更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果 数据表之间已经设置了关联:

1.  用ActiveWriter 设置了 SourceColumn,TargetColumnKey,TagertCascase,TargetLazy ,Target Inverse

2.设置version

 

需要用下面方法更新(数据库里完全没有数据的情况)

 

///// TABLE2 是Table1的子表

            List<Standard> standards = new List<Standard>(Standard.FindAll());

           // System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
           // con.ConnectionString = "Data Source=200.200.200.20;Initial Catalog=FMEA1;Integrated Security=SSPI";
           // using (new Castle.ActiveRecord.Framework.Scopes.DifferentDatabaseScope(con))
           // {
           //     foreach (Standard s in standards)
          //      {
            //        s.Update();
           //     }
          //  }
           // List<Express> levels = new List<Express>(Express.FindAll());

            
            //List<TABLE1> listTable1 = new List<TABLE1>(TABLE1.FindAll());

            
            //List<TABLE2> listTable2 = new List<TABLE2>(TABLE2.FindAll());
            
            ActiveRecordStarter.ResetInitializationFlag();

            IConfigurationSource source2 = System.Configuration.ConfigurationManager.GetSection("access") as IConfigurationSource;
            //开启日志
            //log4net.Config.XmlConfigurator.Configure();
            Assembly assembly = Assembly.Load(@"DBChange");
            ActiveRecordStarter.Initialize(assembly, source2);

         //////   List<Express> levels = new List<Express>(Express.FindAll());

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                   try
                    {
                        foreach (Standard sd in standards)
                        {
                            sd.SaveCopy();                          
                           
                        }
                        

                        ts.VoteCommit();
                        
                   }
                    catch
                    {
                        ts.VoteRollBack();
                       
                    }


                }
            }
            catch
            {
                MessageBox.Show("失败");
            }
            MessageBox.Show("成功");

 

 

 

转载于:https://www.cnblogs.com/ugvanxk/archive/2008/11/28/1343180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值