问题
I'm trying to write Connection string to Web.config like this:
<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?
回答1:
Add reference to add System.Configuration
:-
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
Also you can change the WebConfig file to include the provider name:-
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping;
Integrated Security=True"; providerName="System.Data.SqlClient" />
</connectionStrings>
回答2:
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 { }
回答3:
Are you sure that your configuration file (web.config) is at the right place and the connection string is really in the (generated) file? If you publish your file, the content of web.release.config might be copied.
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>
回答4:
try this
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
回答5:
Try this After open web.config file in application and add sample db connection in connectionStrings section like this
<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/>
</connectionStrings >
回答6:
Try to use WebConfigurationManager
instead of ConfigurationManager
回答7:
After opening the web.config file in application, add sample db connection in connectionStrings section like this:
<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:
<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. Add the following namespace in codebehind file.
using System.Configuration;
This namespace is used to get configuration section details from web.config file.
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();
}
}
来源:https://stackoverflow.com/questions/18431147/how-to-write-connection-string-in-web-config-file-and-read-from-it