开发者

Putting a connection string in the web config

So I'm using C# and I've got the following SQL connection string:

private static string _conn = Properties.Settin开发者_开发知识库gs.Default.dBizConnectionString;

And I'd like to know if and how I can put it in the web config and app config? Thanks for the help!


Here's a blog post from Scott Forsyth explaining everything:

Using Connection Strings from web.config


The short answer is yes, like so:

<connectionStrings>
    <add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Server=ServerName; Initial Catalog=InitialCatalog; User ID=User; Password=Password; MultipleActiveResultSets=True;" />
</connectionStrings>

There's a lot of information out there about connection strings. There are more options you can specify, they can be encrypted, etc. etc., but this will get you started.


Add it to the web config like this:

  <connectionStrings>
     <add name="myConnectionStringName" connectionString="Data Source=mySqlServerInstance;Initial Catalog=myCatalog;User ID=myUserId;Password=myPassword"/>
  </connectionStrings>

Add code to retrieve it:

private static string GetConnectionString()
{
    var config = WebConfigurationManager.OpenWebConfiguration("/myProject");
    var connections = config.ConnectionStrings;
    var settings = connections.ConnectionStrings["myConnectionStringname"];
    string connectionString = settings.ConnectionString;
    return connectionString;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜