user3216114
user3216114

Reputation: 325

How to use variables in all the windows forms?

In my c# project i used many forms and my database is in sql server 2008.
I create one app.config file in that my connection string.

<add name="Courier_Management_System" connectionString="Data Source=.\sqlexpress;Initial Catalog=Courier_Management_System;Integrated Security=True" />

now in all the forms i write the following lines for working with database

SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Courier_Management_System"].ToString();
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();


so my question is if it is possible to write the above all the lines once and use it in all the forms.

Upvotes: 1

Views: 101

Answers (3)

Captain Kenpachi
Captain Kenpachi

Reputation: 7215

The right way to do these things is to have a separate project that does the data access. But the most important idea is that you want the data access to take place in one location, e.g. in a class file like Praveen demonstrated. You can even add extra functions to execute queries and return specific resultsets, e.g.:

class MyDatabase
{
   SqlConnection cnn;
   SqlDataAdapter adp;
   DataSet ds;
   DataTable dt;

   public MyDatabase()
   {
      cnn = new SqlConnection();
      cnn.ConnectionString =          ConfigurationManager.ConnectionStrings["Courier_Management_System"].ToString();
      adp = new SqlDataAdapter();
      ds = new DataSet();
      dt = new DataTable();
   }

   public Dataset ExecuteQuery(string SQL)
   {
         cnn.Open();
         var ret = cnn.Execute(SQL);
         cnn.Close();
         return ret;
   }

}

Upvotes: 1

Sumeshk
Sumeshk

Reputation: 1988

Create a static method in your Program.cs class

Public Static SqlConnection GetConnection()
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = configurationManager.ConnectionStrings["Courier_Management_System"].ToString();
return cnn;
}

you can call this method as Program.GetConnection() where ever you want a connection object, You only need to change the connection string in only one place if there is any change.

No need to add

SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

inside that method , because in most of the time you are not suppose to use those variables .

Upvotes: 0

fullstackduck
fullstackduck

Reputation: 1949

Create a class lets say MyDatabase and in its constructor put your code as above and make these variables public. Now you can create object of this class and use these variables, as these will be initialized as soon as you create the object.

Example code:

class MyDatabase
{
 SqlConnection cnn;
 SqlDataAdapter adp;
 DataSet ds;
 DataTable dt;

 public MyDatabase()
 {
  cnn = new SqlConnection();
  cnn.ConnectionString =     ConfigurationManager.ConnectionStrings["Courier_Management_System"].ToString();
  adp = new SqlDataAdapter();
  ds = new DataSet();
  dt = new DataTable();
 }
}

Using this class

class YourForm
{
 void someMethod()
 {
  MyDatabase myDatabase = new MyDatabase();
  myDatabase.ds = //do something..
  gridView.DataSource = myDatabase.ds; //using the variable..
 }
}

Upvotes: 1

Related Questions