12

我正在尝试将连接字符串写入 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);

运行程序时,由于空引用而出现错误。但是当我使用这段代码时:

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

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

4

7 回答 7

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>
于 2013-08-25T16:33:37.243 回答
10

网络配置:

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

c#代码:

using System.Configuration;
using System.Data

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

try
{
    if(_connection.State==ConnectionState.Closed)
        _connection.Open();
}
catch { }
于 2014-02-10T07:05:53.907 回答
4

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

对我来说,配置和对连接字符串的访问看起来没问题。我总是会添加一个提供者名称

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping; 
       Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
于 2013-08-25T16:55:48.870 回答
3

试试这个 在应用程序中打开 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 >
于 2015-09-03T14:24:05.797 回答
2

尝试这个

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
于 2013-08-25T16:37:29.837 回答
1

尝试使用WebConfigurationManager而不是ConfigurationManager

于 2013-08-25T16:32:22.217 回答
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();  
    }  
}  
于 2018-07-14T14:15:37.137 回答