如何通过 C# 从 App.config 文件中读取连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9859510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read the connection string from App.config file by C#
提问by co2f2e
I'm using this code to read the connection string from my app.config file but it always return a null value. My App.config file is under my project. Both methods are resulting null values:
我正在使用此代码从我的 app.config 文件中读取连接字符串,但它始终返回空值。我的 App.config 文件在我的项目下。这两种方法都会产生空值:
public SqlConnection getConnection()
{
try
{
// connectionString = ConfigurationManager.AppSettings["dbConn"];
connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
connectionString = System.Configuration.ConfigurationManager.AppSettings["dbConn"];
sqlConnection = new SqlConnection(connectionString);
sqlConnection = new SqlConnection(connectionString);
}
catch (Exception ex)
{
}
return sqlConnection;
}
This is my app.config file declaration:
这是我的 app.config 文件声明:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConn" providerName="System.Data.SqlClient"
connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</connectionStrings>
</configuration>
采纳答案by PraveenVenu
Can you please try
你能试试吗
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConn" providerName="System.Data.SqlClient"
connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</connectionStrings>
<appSettings>
<add key="dbConn" value="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</appSettings>
</configuration>
Then use the second method.
然后使用第二种方法。
Make sure you select the App.Config file in the solution explorer and in the property window select Copy to Output Directoryto Copy Always. Now Build the application and try again.
确保您选择在Solution Explorer,并在属性窗口中的App.config文件选择Copy to Output Directory到Copy Always。现在构建应用程序并重试。


回答by Alexander
I think your problem that you try to read connection string twice, first you do it right, and second time you do it wrong, so just remove second line:
我认为你的问题是你尝试读取连接字符串两次,第一次你做对了,第二次你做错了,所以只需删除第二行:
connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
sqlConnection = new SqlConnection(connectionString);
ConfigurationManager.AppSettingsused to access <appSettings>...</appSettings>section of the config.
ConfigurationManager.AppSettings用于访问<appSettings>...</appSettings>配置部分。
回答by Akash KC
You simple define connection string in one class and call that string.....
您只需在一个类中定义连接字符串并调用该字符串.....
public class Connection
{
/// <summary>
/// Connection String
/// </summary>
public static string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
}
}
}
//Returning connction string
sqlConnection conn = new SqlConnection(Connection.ConnectionString);
回答by Stecenko Ruslan
connectionString=global::myProject.Properties.Settings.Default.myConnectionString

