您的位置:首页 > 编程语言

保存NHibernate配置xml文件信息的源代码

2015-04-01 00:03 423 查看
<pre name="code" class="html"><pre code_snippet_id="632860" snippet_file_name="blog_20150401_1_8801928" name="code" class="csharp">hibernate.cfg.xml



<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="CwfServer">
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.connection_string">Server=114.80.156.207;Database=sq_locationinfo;Uid=sq_locationinfo;Pwd=hbc771215;</property>
<property name="default_schema"></property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="command_timeout">60</property>
<property name="hbm2ddl.auto">update</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="LNFrameWork" />
</session-factory>
</hibernate-configuration>


//保存NHibernate配置xml文件信息
string nh_xmlconfigPath = Application.StartupPath + "\\Config\\hibernate.cfg.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(nh_xmlconfigPath);
XmlNodeList hc_node = xmldoc.ChildNodes;
XmlNodeList sf_node = hc_node[1].ChildNodes;
XmlNodeList propList = sf_node[0].ChildNodes;// SelectNodes("hibernate-configuration/session-factory/property");
if (propList != null)
{
foreach (XmlNode propNode in propList)
{
if (propNode.Attributes["name"] != null)
{
string name = propNode.Attributes["name"].Value;
#region  //通过Attributes获得属性名字为name的属性
switch (name)
{
case "connection.driver_class":
switch (this.ucConnInfo.DatabaseType)
{
case DataProviderType.Sql:
propNode.InnerText = typeof(SqlClientDriver).FullName;
break;
case DataProviderType.Oracle:
propNode.InnerText = typeof(OracleClientDriver).FullName;
break;
case DataProviderType.MySQL:
propNode.InnerText = typeof(MySqlDataDriver).FullName;
break;
}
break;
case "dialect":
switch (this.ucConnInfo.DatabaseType)
{
case DataProviderType.Sql:
propNode.InnerText = typeof(MsSql2008Dialect).FullName;
break;
case DataProviderType.Oracle:
propNode.InnerText = typeof(Oracle10gDialect).FullName;
break;
case DataProviderType.MySQL:
propNode.InnerText = typeof(MySQL5Dialect).FullName;
break;
}
break;
case "connection.connection_string":
propNode.InnerText = this.ucConnInfo.ConnInfo;
break;
case "default_schema":
propNode.InnerText = this.ucConnInfo.dbname_userid;
break;
}
#endregion
}
}
}
xmldoc.Save(nh_xmlconfigPath);
xmldoc.Save(nh_xmlconfigPath);

xmldoc = new XmlDocument();
xmldoc.Load(nh_xmlconfigPath);
if (xmldoc.OuterXml.Contains(this.ucConnInfo.ConnInfo) == true)
{
MessageBox.Show("保存完毕!", "提示");
}
else
{
MessageBox.Show("保存出错,请重新保存操作!", "提示");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: