none
How to create connection string and where to save connection string to access from different classes RRS feed

  • Question

  • I have created a windows form in which there is a combobox from where user can select a server from a list of available server. There is two radio button to select authentication mode windows authentication or SQL server authentication. There are two text box to enter user id and password when SQL server authentication mode is selected. There are another combobox which contain list of all database present in selected server. 

    I want to create connection string to use it from different classes to manipulate database.

    If this is not correct forums for this problem then please move this problem to correct forum.

     
    Wednesday, December 27, 2017 8:23 AM

Answers

  • Hi AJret,

    You could save the connection string in a static porperty, then you could call the connection string anywhere from the application.

    For more information about static property, please refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AJret Thursday, December 28, 2017 8:06 AM
    Wednesday, December 27, 2017 9:43 AM
    Moderator
  • Hi AJret,

    First, you could create a class with static property like this:

    public class ConnUtils
        {
            //public static string Authentication { get; set; }
            //public static string ServeName  { get; set; }
            //public static string UserName { get; set; }
            //public static string Password { get; set; }
            //public static string DatabaseName { get; set; }
            public static string ConnectionString { get; set; }
        }


    #Second, on your winform page, you could save connection string in that class like this:

    private void button1_Click(object sender, EventArgs e)
    {
    
                string authentication = ""; //retrieve it from control;
                string ServeName = ""; //retrieve it from control;
                string UserName = ""; //retrieve it from control;
                string Password = ""; //retrieve it from control;
                string DatabaseName = ""; //retrieve it from control;
                if (authentication == "Windows Authentication")
                {
                    ConnUtils.ConnectionString = string.Format("Server={0};Database={1};Trusted_Connection=True;", ServeName, DatabaseName);
                }
                else
                {
                    ConnUtils.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password = {3};", ServeName, DatabaseName, UserName, Password);
                }
    }

    #Third, you could retrieve the connection string anywhere, like this:

    using (var conn = new SqlConnection(ConnUtils.ConnectionString))
    {
    // your code here
    }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AJret Thursday, December 28, 2017 8:06 AM
    Thursday, December 28, 2017 2:09 AM
    Moderator

All replies

  • Hi AJret,

    You could save the connection string in a static porperty, then you could call the connection string anywhere from the application.

    For more information about static property, please refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AJret Thursday, December 28, 2017 8:06 AM
    Wednesday, December 27, 2017 9:43 AM
    Moderator
  • Please give me some example to create connection string for this type of scenario.
    Wednesday, December 27, 2017 9:57 AM
  • Hi AJret,

    First, you could create a class with static property like this:

    public class ConnUtils
        {
            //public static string Authentication { get; set; }
            //public static string ServeName  { get; set; }
            //public static string UserName { get; set; }
            //public static string Password { get; set; }
            //public static string DatabaseName { get; set; }
            public static string ConnectionString { get; set; }
        }


    #Second, on your winform page, you could save connection string in that class like this:

    private void button1_Click(object sender, EventArgs e)
    {
    
                string authentication = ""; //retrieve it from control;
                string ServeName = ""; //retrieve it from control;
                string UserName = ""; //retrieve it from control;
                string Password = ""; //retrieve it from control;
                string DatabaseName = ""; //retrieve it from control;
                if (authentication == "Windows Authentication")
                {
                    ConnUtils.ConnectionString = string.Format("Server={0};Database={1};Trusted_Connection=True;", ServeName, DatabaseName);
                }
                else
                {
                    ConnUtils.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password = {3};", ServeName, DatabaseName, UserName, Password);
                }
    }

    #Third, you could retrieve the connection string anywhere, like this:

    using (var conn = new SqlConnection(ConnUtils.ConnectionString))
    {
    // your code here
    }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AJret Thursday, December 28, 2017 8:06 AM
    Thursday, December 28, 2017 2:09 AM
    Moderator