使用connectionstring从网络。在源代码文件中配置

时间:2022-05-14 15:43:56

I know this might be a very basic question, but maybe thats why I'm having problems finding the answer. Right now I'm creating database connections in my source files by doing something like this:

我知道这可能是一个非常基本的问题,但也许这就是我找不到答案的原因。现在我在源文件中创建数据库连接,方法如下:

SqlConnection con = new SqlConnection("Data Source=...Password=...);
SqlCommand cmd = new SqlCommand(String.Format("SELECT * FROM Table;"), con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

But this means that if I choose to change databases it will be a major pain. Do you guys know how to use the connection string from a web.config file instead?

但这意味着,如果我选择更改数据库,这将是一个巨大的痛苦。你们知道如何使用网络连接字符串吗?配置文件呢?

Thank you!

谢谢你!

6 个解决方案

#1


46  

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);

Make sure that your website has a reference to System.Configuration or it won't work.

确保你的网站有系统参考。配置,否则不行。

The documentation can be found as How to: Read Connection Strings from the Web.config File, with code sample

该文档可以找到如何:从Web读取连接字符串。配置文件,带有代码示例

#2


8  

You can try

你可以试着

var conString = System.Configuration.
                ConfigurationManager.ConnectionStrings["connectionStringName"];
string strConnString = conString.ConnectionString;
SqlConnection con = new SqlConnection(strConnString);

#3


5  

In your app.config (or web.config):

在您的app.config(或web.config)中:

<configuration>
       <connectionStrings>
           <add name="MainConnectString" connectionString="yourConnectionString" providerName="providerName" />
       </connectionStrings>
</configuration>

And in code:

在代码:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"];

#4


2  

along with *Exception's response, make sure to add using System.Configuration; to your code behind page

除了*Exception的响应之外,还要确保使用System.Configuration添加;到页面后面的代码

#5


1  

You can use EnterpriseLibrary.

您可以使用EnterpriseLibrary。

using Microsoft.Practices.EnterpriseLibrary.Data;

Then you create a method to get connection string:

然后创建一个方法来获取连接字符串:

 public static string GetConnectionString()
    {
        Database YourData = DatabaseFactory.CreateDatabase("someconnectionname");
        return YourData .ConnectionString;
    }

In your web.config you will have following:

在你的网络。配置如下:

 <connectionStrings>   
   <add name="someconnectionname" connectionstring=..... />
 </connectionString>

#6


0  

If you get "cannot implicitly convert type 'system.configuration.connectionstringsettings' to 'string'", do:

如果你得到"不能隐式转换类型'system.configuration。connectionstringsettings”到“字符串”,做的事:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"].ConnectionString;

#1


46  

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);

Make sure that your website has a reference to System.Configuration or it won't work.

确保你的网站有系统参考。配置,否则不行。

The documentation can be found as How to: Read Connection Strings from the Web.config File, with code sample

该文档可以找到如何:从Web读取连接字符串。配置文件,带有代码示例

#2


8  

You can try

你可以试着

var conString = System.Configuration.
                ConfigurationManager.ConnectionStrings["connectionStringName"];
string strConnString = conString.ConnectionString;
SqlConnection con = new SqlConnection(strConnString);

#3


5  

In your app.config (or web.config):

在您的app.config(或web.config)中:

<configuration>
       <connectionStrings>
           <add name="MainConnectString" connectionString="yourConnectionString" providerName="providerName" />
       </connectionStrings>
</configuration>

And in code:

在代码:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"];

#4


2  

along with *Exception's response, make sure to add using System.Configuration; to your code behind page

除了*Exception的响应之外,还要确保使用System.Configuration添加;到页面后面的代码

#5


1  

You can use EnterpriseLibrary.

您可以使用EnterpriseLibrary。

using Microsoft.Practices.EnterpriseLibrary.Data;

Then you create a method to get connection string:

然后创建一个方法来获取连接字符串:

 public static string GetConnectionString()
    {
        Database YourData = DatabaseFactory.CreateDatabase("someconnectionname");
        return YourData .ConnectionString;
    }

In your web.config you will have following:

在你的网络。配置如下:

 <connectionStrings>   
   <add name="someconnectionname" connectionstring=..... />
 </connectionString>

#6


0  

If you get "cannot implicitly convert type 'system.configuration.connectionstringsettings' to 'string'", do:

如果你得到"不能隐式转换类型'system.configuration。connectionstringsettings”到“字符串”,做的事:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"].ConnectionString;