如何在web.config文件中编写连接字符串并从中读取?

时间:2013-08-25 16:28:03

标签: c# asp.net sqlconnection

我正在尝试将连接字符串写入Web.config,如下所示:

<connectionStrings>
  <add name="Dbconnection" connectionString="Server=localhost; 
       Database=OnlineShopping ; Integrated Security=True"/>
</connectionStrings >

并从中读取:

string strcon = 
    ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);

运行程序时因为null引用而出错。但是当我使用这段代码时:

SqlConnection DbConnection = new SqlConnection();
DbConnection.ConnectionString = 
    "Server=localhost; Database=OnlineShopping ; Integrated Security=True";

我没有收到任何错误,程序正常运行! 有什么问题?

7 个答案:

答案 0 :(得分:11)

添加引用以添加System.Configuration: -

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

您还可以更改WebConfig文件以包含提供程序名称: -

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping;
       Integrated Security=True"; providerName="System.Data.SqlClient" />
</connectionStrings>

答案 1 :(得分:9)

的Web.config:

<connectionStrings>
    <add name="ConnStringDb" connectionString="Data Source=localhost;
         Initial Catalog=DatabaseName; Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

c#c​​ode:

using System.Configuration;
using System.Data

SqlConnection _connection = new SqlConnection(
          ConfigurationManager.ConnectionStrings["ConnStringDb"].ToString());

try
{
    if(_connection.State==ConnectionState.Closed)
        _connection.Open();
}
catch { }

答案 2 :(得分:4)

您确定您的配置文件(web.config)位于正确的位置且连接字符串确实在(生成的)文件中吗?如果您发布文件,则可能会复制web.release.config的内容。

配置和对Connection字符串的访问看起来很合适。我总是会添加一个提供商名称

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping; 
       Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

答案 3 :(得分:2)

试试这个

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();

答案 4 :(得分:2)

试试这个 在应用程序中打开web.config文件并在connectionStrings部分中添加示例数据库连接,如下所示

<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/>
</connectionStrings >

答案 5 :(得分:1)

尝试使用WebConfigurationManager代替ConfigurationManager

答案 6 :(得分:0)

在应用程序中打开web.config文件后,在connectionStrings部分中添加示例数据库连接,如下所示:

<connectionStrings>  
    <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient" />   
</connectionStrings>  

在web.config文件中声明connectionStrings:

 <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient" />   

不需要用户名和密码即可访问数据库服务器。 现在,编写代码以从我们的代码隐藏文件中的web.config文件获取连接字符串。在代码隐藏文件中添加以下名称空间。

using System.Configuration;

此命名空间用于从web.config文件获取配置节详细信息。

using System;  
using System.Data.SqlClient;  
using System.Configuration;  
public partial class _Default: System.Web.UI.Page {  
    protected void Page_Load(object sender, EventArgs e) {  
        //Get connection string from web.config file  
        string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;  
        //create new sqlconnection and connection to database by using connection string from web.config file  
        SqlConnection con = new SqlConnection(strcon);  
        con.Open();  
    }  
}