<?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("失败");
}
}