如何为.cscfg文件中的Windows azure - worker角色设置数据库连接字符串..?

时间:2021-08-24 11:20:30

Have to set the Database connection string in the .cscfg file, not the app.config (In my windows azure worker role application). Can anyone help me on this..?

必须在.cscfg文件中设置数据库连接字符串,而不是app.config(在我的windows azure worker角色应用程序中)。谁可以帮我这个事..?

Thanks in Adavance. Cheers.

谢谢Adavance。干杯。

1 个解决方案

#1


3  

If you're thinking about setting the connection string in <connectionStrings></connectionStrings> element in .cscfg file, you can't do that. Configuration file only takes name/value pair. So you would do something like:

如果您正在考虑在.cscfg文件中的 元素中设置连接字符串,则无法执行此操作。配置文件只接受名称/值对。所以你会做类似的事情:

<?xml version="1.0"?>
<ServiceConfiguration serviceName="Web.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="3" osVersion="*" schemaVersion="2013-03.2.0">
  <Role name="Worker">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="DatabaseConnectionString" value="your database connection string" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

and then read the connection string in your code like:

然后读取代码中的连接字符串,如:

var databaseConnectionString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")

and use that connection string for your database calls.

并使用该连接字符串进行数据库调用。

#1


3  

If you're thinking about setting the connection string in <connectionStrings></connectionStrings> element in .cscfg file, you can't do that. Configuration file only takes name/value pair. So you would do something like:

如果您正在考虑在.cscfg文件中的 元素中设置连接字符串,则无法执行此操作。配置文件只接受名称/值对。所以你会做类似的事情:

<?xml version="1.0"?>
<ServiceConfiguration serviceName="Web.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="3" osVersion="*" schemaVersion="2013-03.2.0">
  <Role name="Worker">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="DatabaseConnectionString" value="your database connection string" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

and then read the connection string in your code like:

然后读取代码中的连接字符串,如:

var databaseConnectionString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")

and use that connection string for your database calls.

并使用该连接字符串进行数据库调用。