简体   繁体   中英

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

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.

Look at: How to set SQL Server connection string?

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

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.

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!!!

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM