zoukankan      html  css  js  c++  java
  • 关于C#中hibernate.cfg.xml动态加载问题

    由于项目需要,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>这个节点可以空着了,安全问题也同时解决了 啦啦啦

  • 相关阅读:
    帮忙看看怎么优化这个最长的sql
    12种不宜使用的Javascript语法
    走格子
    乘法逆元
    完美字符串
    全排列问题
    A. Sorting Railway Cars
    Prim
    矩阵取数
    套题T8&T9
  • 原文地址:https://www.cnblogs.com/yannis/p/2020900.html
Copyright © 2011-2022 走看看