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

前端 未结 10 1134
遇见更好的自我
遇见更好的自我 2020-12-06 00:27

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

app.config code

   
           


        
相关标签:
10条回答
  • 2020-12-06 00:42
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ToString()))
    {
    ....(your code here) ...
    }
    
    0 讨论(0)
  • 2020-12-06 00:45

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

    SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);       
    
    0 讨论(0)
  • 2020-12-06 00:49

    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.

    0 讨论(0)
  • 2020-12-06 00:54

    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;
    
    0 讨论(0)
提交回复
热议问题