【问题标题】:Microsoft Data Application Block passing data configuration pathMicrosoft 数据应用程序块传递数据配置路径
【发布时间】:2012-08-06 12:31:43
【问题描述】:

我目前正在为所有与数据库相关的操作使用 Microsoft 数据应用程序块构建应用程序。

我在 web.config 文件中有数据配置的配置设置,如下所示。

Web.config

<storageProvider xsi:type="XmlFileStorageProviderData" name="XML File Storage Provider" path="dataConfiguration.config"/>
    <dataTransformer xsi:type="XmlSerializerTransformerData" name="Xml Serializer Transformer">
      < include Types/>
    </data Transformer >
  </configuration Section >

代码

db = DatabaseFactory.CreateDatabase(databaseInstance);                
db.ExecuteNonQuery(CommandType.Text, sqlQuery);                

我能够成功连接到数据库,查询等。

但是,我们在不同的服务器上有不同的配置文件。

有什么方法可以在代码中传递路径属性,并能够通过加载特定的配置文件而不是在 web.config 中硬编码来与适当的数据库实例交互??

【问题讨论】:

    标签: asp.net web-applications web-config


    【解决方案1】:

    您好,您可以使用环境变量并加载好的配置

    1 在你的机器Environment上创建环境变量

    环境可以有不同的值:暂存、生产等

    2 使用此代码获取您的变量

    var variable = System.Environment.GetEnvironmentVariable("YourNameVariable")
    if(variable == "STG") //Staging
    {
       //Load your string connection of staging
    }
    else if(variable == "PRD") //Production
    {
       //Load your string connection of production
    }
    

    3 在您的文件中创建关于配置的不同部分并使用您调整的环境变量加载部分目标。

    【讨论】:

    • Dint 很明白我应该怎么做。你能指出我的示例代码/示例吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多