0

I want to make a single connection file , using which , all the Forms of my winform application should connect to the online mysql database and select,update & insert data.

I have named the connection file as CONNECTION.CS, connection string is :

OdbcConnection conn = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306"); 

Now , how do I use it in the Form1.cs,Form2.cs ..........to establish connection to the database and start inserting and retreiving data? Please help.

Do i need to inherit this Connection.cs in all the Forms? Please help with code

1
  • Don't leave the connection open for the whole program's lifetime. This is a bad design. Open it when you need it and close it when you don't Commented Jun 26, 2011 at 8:23

1 Answer 1

2

I think it will be easer if you define it in the app.config file

<appSettings>
  <add key="ConnectionString" value="Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306" />
   </appSettings>
</configuration>

so whenever you want to get a connectionstring you can get it :

string strConn = ConfigurationManager.AppSettings["ConnectionString"];

or you can use a class as a data access layer :

class Connection
{
     OleDbConnection conn;
     OleDbCommand cmd;
     public Connection()
     {
          string connnstr = "Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306";
          conn = new OleDbConnection(connstr);
          cmd = new OleDbCommand();
          cmd.Connection = conn;
     }
     public OleDbDataReader GetData()
     {
        ....
     }
}

then whenever you want to getdata just

Connection conn = new Connection();
OleDbDataReader dr = conn.getData();

by this way you just define a single connection file.

Sign up to request clarification or add additional context in comments.

4 Comments

ok sir, but if this file is visible when i install my application on my customer's pcs , then if he / she deletes it OR changes some code in it by mistake then it would be a big problem? so how do i deal with this?
ok i edited the answer and i added an alternative way to define a single connection string in a class that represents the data access layer.
@sqlchild the following blog post shows you how to encrypt specific sections of the app.config, so that your customers wouldn't be able to read the connection string. This is a best practice for any connection strings in the web.config too. dotnetprofessional.com/blog/post/2008/03/…
I believe it is best to keep it in the app.config and encrypt the connectionstring section. Compiled code can be easily decompiled, especially with tools like .Net reflector.

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.