由于项目需要,hibernate.cfg.xml里的链接字符串需要动态加载,在网上找好久,无满足答案。
自己摸索2个方法
1、修改xml文件
string FileName = "hibernate.cfg.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(FileName);
XmlNodeList nodeList = xmldoc.DocumentElement.ChildNodes[0].ChildNodes;
foreach (XmlNode nl in nodeList)
{
if (nl.Name == "property" && nl.Attributes["name"].Value == "connection.connection_string")
{
nl.InnerXml = @" 数据库连接字符串;";
break;
}
}
XmlTextWriter writer = new XmlTextWriter(FileName, Encoding.Default);
writer.Formatting = Formatting.Indented;
xmldoc.Save(writer);
不管用啊,坑爹呢,在 configuration = new Configuration().Configure("hibernate.cfg.xml");加载会报出,An exception occurred during configuration of persistence layer.这个破异常,直接无视,然后自己摸索到第二个方法
configuration = new Configuration().Configure("hibernate.cfg.xml")
.SetProperty("connection.connection_string", " 数据库连接字符串");
OK搞定,hibernate.cfg.xml中的<property name="connection.connection_string"></property>这个节点可以空着了,安全问题也同时解决了 啦啦啦