简体   繁体   English

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

[英]How to write connection string in web.config file and read from it?

I'm trying to write Connection string to Web.config like this:我正在尝试像这样将连接字符串写入 Web.config:

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

and read from it like this:并像这样阅读它:

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

when run the program I get an error because of the null reference.运行程序时,由于空引用而出现错误。 but when I use this code:但是当我使用此代码时:

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

I don't get any error and the program works correctly!我没有收到任何错误,程序正常运行! What is the problem?问题是什么?

Add reference to add System.Configuration :-添加引用以添加System.Configuration :-

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

Also you can change the WebConfig file to include the provider name:-您也可以更改 WebConfig 文件以包含提供程序名称:-

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

Web.config:网页配置:

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

c# code:代码:

using System.Configuration;
using System.Data

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

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

Are you sure that your configuration file (web.config) is at the right place and the connection string is really in the (generated) file?您确定您的配置文件 (web.config) 位于正确的位置并且连接字符串确实在(生成的)文件中吗? If you publish your file, the content of web.release.config might be copied.如果您发布文件,则可能会复制 web.release.config 的内容。

The configuration and the access to the Connection string looks all right to me.配置和对连接字符串的访问在我看来没问题。 I would always add a providername我总是会添加一个提供者名称

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

Try this After open web.config file in application and add sample db connection in connectionStrings section like this在应用程序中打开 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 >

try this试试这个

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

尝试使用WebConfigurationManager而不是ConfigurationManager

After opening the web.config file in application, add sample db connection in connectionStrings section like this:在应用程序中打开 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>  

Declaring connectionStrings in web.config file:在 web.config 文件中声明 connectionStrings:

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

There is no need of username and password to access the database server.无需用户名和密码即可访问数据库服务器。 Now, write the code to get the connection string from web.config file in our codebehind file.现在,编写代码以从我们的代码隐藏文件中的 web.config 文件中获取连接字符串。 Add the following namespace in codebehind file.在代码隐藏文件中添加以下命名空间。

using System.Configuration;

This namespace is used to get configuration section details from web.config file.此命名空间用于从 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();  
    }  
}  

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

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