ActiveRecord切换数据库导数据

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
      <section name="mssqlserver" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
      <section name="oracle" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
      <section name="access" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />     
    </configSections>
  <mssqlserver>
    <config>
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.connection.connection_string" value="Data Source=200.200.200.20;Initial Catalog=FMEA;Integrated Security=SSPI" />
    </config>
  </mssqlserver>
  <oracle>
    <config>
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect" />
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.connection.connection_string" value="Data Source=Derating;User ID=system;Password=password;" />
    </config>
  </oracle>
  <access>
    <config>
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.connection.connection_string" value="Data Source=200.200.200.20;Initial Catalog=FMEA1;Integrated Security=SSPI" />
    </config>
  </access>
</configuration>

 IConfigurationSource source1 = System.Configuration.ConfigurationManager.GetSection("mssqlserver") as IConfigurationSource;

            IConfigurationSource source2 = System.Configuration.ConfigurationManager.GetSection("access") as IConfigurationSource;
            //开启日志
            //log4net.Config.XmlConfigurator.Configure();
            Assembly assembly = Assembly.Load(@"DBChange");
            ActiveRecordStarter.Initialize(assembly, source1);   
           
       
        更改数据库
            / TABLE2 是Table1的子表
           
           
            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);

            using (TransactionScope  ts=new TransactionScope())
            {
                try
                {
                    foreach (TABLE1 tb1 in listTable1)
                    {
                        TABLE1 ntb1 = TABLE1.TryFind(tb1.Table1ID);

                        因为Version的问题,所以首先必须删除原来的。否则save or update 会提示
                        Row was updated or deleted by another transcation
                        if (ntb1 != null)
                        {
                            // 首先删除子表
                            foreach (TABLE2 tb2 in ntb1.TABLE2s)
                            {
                                tb2.Delete();
                            }
                            ntb1.Delete();
                        }

                        tb1.Create();


                        子表
                        foreach (TABLE2 tb2 in tb1.TABLE2s)
                        {
                            tb2.Create();
                        }


                    }                


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

                }

            }

转载于:https://www.cnblogs.com/ugvanxk/archive/2008/11/07/1328570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值