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

  • 相关阅读:
    Java代码检测工具
    java编程工具
    100-days: The one day
    前端书籍整理
    vue 学习笔记(二)
    从零开始写一个npm包及上传
    Vue Baidu Map 插件的使用
    对数组对象进行过滤
    使用css方法使footer保持在页面的最底部
    判断是第一次打开界面?还是刷新
  • 原文地址:https://www.cnblogs.com/yannis/p/2020900.html
Copyright © 2020-2023  润新知