简体   繁体   English

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

[英]How to set Database connection string for Windows azure - worker role in .cscfg file..?

Have to set the Database connection string in the .cscfg file, not the app.config (In my windows azure worker role application). 必须在.cscfg文件而不是app.config中设置数据库连接字符串(在我的Windows Azure工作角色应用程序中)。 Can anyone help me on this..? 谁可以帮我这个事..?

Thanks in Adavance. 感谢Adavance。 Cheers. 干杯。

If you're thinking about setting the connection string in <connectionStrings></connectionStrings> element in .cscfg file, you can't do that. 如果您正在考虑在.cscfg文件的<connectionStrings></connectionStrings>元素中设置连接字符串,则不能这样做。 Configuration file only takes name/value pair. 配置文件仅包含名称/值对。 So you would do something like: 因此,您将执行以下操作:

<?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. 并使用该连接字符串进行数据库调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM