Accessing database connection string using app.config in C# winform

独自空忆成欢 提交于 2019-11-26 20:52:26

问题


I can't seem to be able to access the app.config database connection string in my c# winforms app.

app.config code

   <connectionStrings>
      <add name="MyDBConnectionString" providerName="System.Data.SqlClient"
            connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
   </connectionStrings>  

C# code:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["MyDBConnectionString"];    

When I try the C# code, I get a message:
Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: ' This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

However, when I try to use:

conn.ConnectionString = System.Configuration!System.Configuration.ConfigurationManager.AppSettings["MyDBConnectionString"];  

I get an error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement


回答1:


This is all you need:

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



回答2:


Use ConfigurationManager instead of ConfigurationSettings. It has a ConnectionStrings property that you should use for connection strings in the connectionStrings section:

ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;



回答3:


You are using the ConnectionStrings collection, not the AppSettings.

ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString;



回答4:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>

<add key="ConnectionString" value="Data Source=MY-PC;Initial Catalog=DB2013;User ID=sa;Password=MYSQL123" />

</appSettings>

</configuration>
using System.Configuration;
using System.Data.SqlClient;

namespace OnlineDelete_W2013
{
public partial class CommodityEdit : Form
{
   SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

    public CommodityEdit()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
           MyConnection.Open();
        }
        catch (Exception)
        {

            throw;
        }



回答5:


try this

 ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString; 



回答6:


using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ToString()))
{
....(your code here) ...
}



回答7:


SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager
    .ConnectionStrings["MyDBConnectionString"].ConnectionString;
try
{
    conn.Open();                
}
catch (Exception)
{
    throw;                    
}



回答8:


The answers stating to use the line

ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

are correct.

If an error appears stating that ConfigurationManager does not exist, it's because your project hasn't referenced System.Configuration.

To do that in .NET Framework, in Solution Explorer, in the project where you want to use this line of code, right-click in References, choose Add Reference..., then choose Assemblies on the left-side and Framework under it. Pick System.Configuration on the list and click Ok.




回答9:


About this:

I get an error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

I just declared a var like this and solved the problem:

var strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;



回答10:


Please try below code. this is as you are expecting:

SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);       


来源:https://stackoverflow.com/questions/8476103/accessing-database-connection-string-using-app-config-in-c-sharp-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!