Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm building a database application, through c# using windows application.

I have different forms and I want to open the same connection in all forms instead of opening the connection in each form and closing it with the form close

Thank you
Posted
Updated 25-Aug-10 12:18pm
v2
Comments
Dalek Dave 25-Aug-10 18:18pm    
Minor Edit for Grammar.
Yusuf 25-Aug-10 18:59pm    
What is the point of defeating the system. You are thinking it all backwards. No, that is way wrong

You are doing it the wrong way already!

SQL Connections are a very scarce resource. By opening the connection on form load and closing it on form close, you are denying it to other processes - this may prevent your own application from working, under some circumstances and you will have no idea why: "It works fine in the development environment"

Open the connection when you need it. Close it as soon as you have finished, and dispose of it as quickly as possible.
 
Share this answer
 
Yes, as stated by OriginalGriff,

in case of ADO.NET, we work basically in disconnected mode. So we take all the informations required for use at a time into DataSet and later manipulate there. Finally we pass the dataset to update itself with the database.

So Create a static class outside any form, and expose few methods to get DataTable/dataset or execute a query. When data is required from the database use this interface which will in turn connect to database using SqlConnection and return you the correct data.

When you want to update, send the dataset to the exposed static method, and update the same.

By this way you are not holding your connection and also update everything from a common place. We call it as DataLayer. :thumbsup:
 
Share this answer
 
Comments
Dalek Dave 25-Aug-10 18:19pm    
Good call.
ok..you'd always have to open and close your connections everytime you use them..but if you want to implement the write-once-run-everywhere style..then fortunately .NET come's with the ref keyword to help you..so what you can do is this..

1. create a class and put in a method for connecting to the database..example
C#
.
.
static class DB
{
   public static void connectDB(ref SqlConnection conn, ref SqlCommand cmd)
   {
      conn = new SqlConnection();
      conn.ConnectionString="[input connection string here]";

      cmd.Connection=conn;
      cmd.CommandType=CommandType.StoredProcedure;
                           //Im assuming a stored procedure
   }
}


So all you just have to do is to call this method inside the classes you want to use it, and it will do all the above for you without you re-typing them everytime..

Example if I want to use it in a different class named useHere, I would do this..

.
.
class useHere
{
  /* First instantiate the connection and command objects because you'll just refer to them using the ref keyword */

  SqlConnection conn;
  SqlCommand cmd;

  /* Now assuming I want to add data to a table */

  public void addData(ref conn, ref cmd)
  {
     DB.connectDB(conn,cmd);
     cmd.CommandText="[input stored proc. name here]";
     conn.Open();
     cmd.ExecuteNonQuery();
     if(conn.State == ConnectionState.Open)
       {
          conn.Close();
       }

     cmd.Dispose();
  }


and you're game.. :) . If you like it, accept answer and VOTE!
 
Share this answer
 
v2
Comments
Dalek Dave 30-Aug-10 8:10am    
Comprehensive answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900