How to use sql connection information from existing app.config file in my C# Project

connection string in app.config c# windows application
connection string in app.config c# console application
get connection string from app.config c#
c# app.config connection string sql server example
how to get connection string from app.config in c# windows application
encrypt connection string in app.config c#
app.config connection string with username and password
get connection string from web.config c#

I am trying to write a small application in C#, where I need to connect to SQL to fetch some data, for that I need SQL user id and password, for same client there are other applications running which gets connected to SQL Server and have config file in encrypted form. I am not very good in C# and .Net, and have difficulty of understanding these concepts, I tried to search in post for answers but didn't get any suitable reply for my question. Can I use that already in use config file in my new project, and read user is and password information which are in encrypted form and use to connect to SQL server?

Any good stuff for understanding the use of config files in C# will be much appreciated, with good examples.

If you have administrative access to the SQL server, you can create an account that you can use for your connection.

If you have no admin rights, then you have to ask the admin to create an account for you... or tell you the credentials you can use.

ConfigurationManager.ConnectionStrings Property (System , If you have administrative access to the SQL server, you can create an account that you can use for your connection. If you have no admin  Get and set c# application settings in runtime and learn how to read connectionstring from app config file in Visual Studio. Using the app.config in c# for creating global connectionstring and how

Look at: How to set SQL Server connection string?

the example is for MSSQL, for others it will be nearly the same.

How to use sql connection information from existing app.config file in , a connection string in the app.config file | Test your C# code online with . Project Type : using (SqlConnection cnn = new SqlConnection(​connectionString)) { 30): The name 'ConfigurationManager' does not exist in the current context  Can I use that already in use config file in my new project, and read user is and password information which are in encrypted form and use to connect to SQL server? Any good stuff for understanding the use of config files in C# will be much appreciated, with good examples.

Use this function

 /// <summary>
            /// Get values of the connection string in the following order :
            /// Server , Database,
            /// If the length of the list is 3 then the 3rd object is True (Integrated Security)
            /// If it is 4 then the 3rd object is the 3rd object is the username and the 4th is the password.
            /// 
            /// </summary>
            /// <returns></returns>
            public static List<string> GetCurrentConnectionString()
            {
                try
                {
                    List<string> lstConnStrData = new List<string>();
                    string[] aryConnStrData = new string[1];
                    // OPen the Config file as XML document and loop over it.
                    XmlDocument XmlDoc = new XmlDocument();
                    //Loading the Config file
                    XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    foreach (XmlElement xElement in XmlDoc.DocumentElement)
                    {
                        if (xElement.Name == "connectionStrings")
                        {
                            //setting the coonection string
                            aryConnStrData = xElement.ChildNodes[1].Attributes[1].Value.Split(';');
                            break;
                        }

                    }

                    // Now loop over the array that holds the conn str data and split each item on "=",
                    // and add the second splitted item to the list, so at the end the list will contains 
                    // the values of the connection string.

                    for (int i = 0; i <= aryConnStrData.Length - 2; i++)
                    {
                        lstConnStrData.Add(aryConnStrData[i].Split('=')[1]);
                    }
                    return lstConnStrData;
                }
                catch (Exception)
                {

                    throw;
                }
            }

OR

   public static List<string> SplitConnectionString(string ConnectionString)
    {
        try
        {
            List<string> lstConnStrData = new List<string>();
            string[] aryConnStrData = new string[1];

            aryConnStrData = ConnectionString.Split(';');

            // Now loop over the array that holds the conn str data and split each item on "=",
            // and add the second splitted item to the list, so at the end the list will contains 
            // the values of the connection string.

            for (int i = 0; i <= aryConnStrData.Length - 2; i++)
            {
                lstConnStrData.Add(aryConnStrData[i].Split('=')[1]);
            }
            return lstConnStrData;
        }
        catch (Exception)
        {

            throw;
        }
    }

Putting a connection string in the app.config file, " and can be located in your project at the same level as e.g. Program. cs. Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file. Child elements include add, clear, and remove. The following configuration file fragment demonstrates the schema and syntax for storing a connection string. The name attribute is a name that you

In the app.config, add the follow lines inside the <configuration>:

<connectionStrings>
  <add name="PWD" connectionString="#$@AERG#$"/>
</connectionStrings>

And in the main program:

  1. Add the reference System.Configuration through the Project Manager.
  2. Add using System.Configuration; in the using segment.
  3. use ConfigurationManager.ConnectionStrings["PWD"].ToString() to access the encrypted password in the config file.

Please change it accordingly to fit your purpose!!!

Good luck!!!

What is App.config in C#.NET? How to use it?, How do I add a config file in Visual Studio 2017? Please Sign up or sign in to vote. This article tells the beginners the basic concepts about connecting to the SQL Server database using C#. This article is for the beginners, to explain the basic concept of data in .NET Framework and how can you use the SQL client in your application to connect to a database.

How to add an app.config file to a project, First of all you should set the connection string in the App.Config file. After that you use the connection string in your forms using this code: associated source code and files, is licensed under The Code Project Open License (CPOL) SqlConnection con = new SqlConnection(ConfigurationManager. Steps to configure 1. Open Visual Studio and create new a Windows application. (Path : Start -> All Programs -> Microsoft Visual Studio 2005-> Microsoft Visual Studio 2005) 2. Add App.config file to the project. 3. Add the following code in the connection string.

How to get Connection String from App.Config in C#, I have added an app.config file to my visual studio pro SDK project and I'm trying to Net Framework 4.6.1, ArcGIS Pro SDK 2.1, SQL Server. Name: Name for the connection string entry which can be used to reference that connection string. ConnectionString: The connection string that contains the specification to make the connection to the underlying database. ProviderName: The name of the ADO.NET provider which can be used to access the underlying database.

Store Database Connection String in App.config for an ArcGIS Pro , Defining Configuration manager in C# how to use connection string using app sql server Duration: 4:48 Posted: Aug 14, 2016 Points covered: 1. Defining Configuration manager in C# how to use connection string using app config file How to Use Database ConnectionString|App config

Comments
  • Yeah this could be an option, let me check with client. As usually they prefer for config files.
  • Thanks for your reply, but actually I want to reuse the config file having encrypted user id and password, read those user id and password from config file of other application and use in my code to connect to SQL server,I have no issue in SQL connection as with hard coded values, it is working perfect.
  • just join these informations and set it as connectionstring: e.g.: conn.ConnectionString = "Data Source=" + ipAddress + ";Initial Catalog=" + databaseName + ";User id=" + decryptedUser + ";Password=" + decryptedPassword +";";
  • Please see if my answer helped you!?
  • instead of putting encrypted password in config file can't I read it directly from the config file of other application like, EMailSetting.config is a config file, it has settings related to email, my app is also sending email and will use same information for sending email as in EMailSetting.config.