Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to c#

Is it possible in c# like vb to open connection one time in module and use through out application in windows form application.

Where to open connection in c# windows form application.

Please help me.

Thanks in Advance.
Posted
Updated 6-Jun-11 21:56pm
v3
Comments
Sergey Alexandrovich Kryukov 7-Jun-11 0:44am    
There is not such language as C#.NET.
--SA
Wild-Programmer 7-Jun-11 1:20am    
I am also sometimes confused with that. Please correct me if I am wrong, .NET is a framework and VB, C#, F# etc are languages. Right?
Sergey Alexandrovich Kryukov 7-Jun-11 0:45am    
What do you mean by connection?
--SA

Yes it is possible. The closest equivalent to a VB module is a static class in C# 2.0 (or a sealed C# class with static members prior to 2.0).
 
Share this answer
 
Just like in VB, you can place the Connection.Open string somewhere in a global class in the data access layer.
You can make this class a singleton, if you want only one instance of this class to exist.
 
Share this answer
 
Hope link1[^] and Link2[^]might help you.
 
Share this answer
 
Why you want to keep connection open?
 
Share this answer
 
hi
SqlConnection cs1 = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "write ur sql select query here";
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query

            cs1.Close();

this will help
 
Share this answer
 
v2
hi
SqlConnection cs1 = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "write ur sql select query here";
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query

            cs1.Close();

this will help
 
Share this answer
 
Use App.Config file for connection open. So that you can use this connection where ever you want (the connection is available for entire application)
 
Share this answer
 
v2

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