如何在web.config文件中写入连接字符串并从中读取
本文关键字:字符 连接 字符串 串并 读取 web config 文件 | 更新日期: 2023-09-27 18:28:54
我正在尝试将连接字符串写入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";
我没有收到任何错误,程序运行正常!问题出在哪里?
添加引用以添加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>
Web.config:
<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 { }
试试这个 在应用程序中打开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)在正确的位置,并且连接字符串确实在(生成的)文件中吗?如果发布文件,则可能会复制web.release.config的内容。
配置和对连接字符串的访问对我来说很好。我总是添加一个名为的提供者
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping;
Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
尝试这个
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
尝试使用WebConfigurationManager
而不是ConfigurationManager
在应用程序中打开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" />
访问数据库服务器不需要用户名和密码。现在,编写代码从我们的codeehind文件中的web.config文件中获取连接字符串。在codebehind文件中添加以下命名空间。
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();
}
}
添加连接字符串的两种方法
如果在安装mssql时设置了实例名称,请确保在web.config文件中正确提供实例名称
- 打开web.config文件
Two ways of adding connection string
if you set up an instance name while installing mssql make sure to provide the instance name correctly in your web.config file
1. open your web.config file
<connectionStrings>
<add name ="EmployeeAppDB"
connectionString="Data Source=INSTANCE_NAME_HERE; <==========
Initial Catalog=EmployeeDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
2. open your web.config file
if you're using the default insallation of mssql with no instance name
<connectionStrings>
<add name ="EmployeeAppDB" connectionString="Data Source=.; <=========
Initial Catalog=EmployeeDB;Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>