0

In C# WinForms, where should I put my SQL connection string variable if I want to access it all over my application?

Right now I'm copy-pasting it wherever I'm using it.

//Sql connection string

SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'");
SqlCommand command = con.CreateCommand();

3 Answers 3

1

You can simply use the app.config (or web.config) for that. It has a special section for connection-strings. See the MSDN-article about that. Then you can retrieve the string in code as use1515791 has already pointed out, like this:

ConfigurationManager.ConnectionStrings["NameOfConnectionStringInConfig"].ConnectionString
Sign up to request clarification or add additional context in comments.

Comments

1

I rather not using app.config file, because it's not secure and hackable easily (every connection info is stored in plain text format here).

If I'm not using any of my DLLs, I place the SqlConnection in Program.cs with public getter and disabled setter. I make all the initializations in Program.cs, so it looks something like this:

static class Program
    {
        private static SqlConnection con;
        internal static SqlConnection Con
        {
            get
            {
                return con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'");
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

Now can access it (for example) from you Form1.cs:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Program.Con;
        }
    }

I've created a DLL to connect to SQL easily, check it here if you're interested.

Comments

0

Thats a rather general question imo.. depends on the application structure and so on. I tend to use a class "ApplicationContext" with properties for such things. However, maybe

ConfigurationManager.ConnectionStrings["whatever"].ConnectionString

is used a lot i think (add reference to System.Configuration)

Btw, if you use it "all over the place", don't you need an adapter class instead?

EDIT

public class DataAcces
{
    private string Connectionstr;
    public DataAcces()
    {
        this.Connectionstr = ConfigurationManager.ConnectionStrings["whatever"].ConnectionString;
    }
    public object ReturnSomething()
    {
         using(SqlConnection con = new SqlConnection(this.Connectionstr))
         {
         //do stuff to db and return something
         }
    }
 }

and then when you need it

....
{
    var whatYouNeed = (new DataAcces()).ReturnSomething();
}

typos happen :) this is the easiest to use i can come up with, not the best.

7 Comments

I have 3 Forms that all use sql queries which requires a connectionstring.
When you say adapter class, do you mean creating a global connectionstring. Like some of my string already has "globalvariables" ?
No, i mean a class thats responsable for all data access. So that all sql is defined in that class.
But how do you call the connection class inside a void ?
I know that I can make a public void and call et from another void like this: sqlcount(sender, e); But I dont think this can be used as a connectionstring variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.