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

10

22

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

Pipistrelle answered 12/12, 2011 at 14:48 Comment(1)
Usually I get that error when I don't put a () at the end of the line of code... (This is old, but I still like to comment)Procora
B
34

This is all you need:

System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
Bandaid answered 12/12, 2011 at 14:51 Comment(0)
I
9

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;
Inositol answered 12/12, 2011 at 14:50 Comment(0)
N
6

You are using the ConnectionStrings collection, not the AppSettings.

ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString;
Newel answered 12/12, 2011 at 14:50 Comment(0)
C
5
<?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;
            }
        }
    }
}
Chopper answered 18/4, 2013 at 7:9 Comment(0)
S
4

try this

 ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString; 
Supertanker answered 12/12, 2011 at 14:50 Comment(0)
T
4
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ToString()))
{
....(your code here) ...
}
Thermoplastic answered 7/6, 2012 at 10:57 Comment(0)
T
2
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager
    .ConnectionStrings["MyDBConnectionString"].ConnectionString;
try
{
    conn.Open();                
}
catch (Exception)
{
    throw;                    
}
Teetotum answered 23/4, 2013 at 2:33 Comment(0)
G
2

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.

Gertudegerty answered 18/3, 2018 at 14:50 Comment(0)
D
0

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;
Detonate answered 3/1, 2019 at 20:14 Comment(0)
U
-1

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

SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);       
Undersexed answered 13/10, 2018 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.