Hi guys,
I am a beginner in C#. I want to create a class which will give a connection to my SQL database and I want to use this connection on a button event in another form.How can I accomplish this? Please help me out.
Thanks in advance
Hi guys,
I am a beginner in C#. I want to create a class which will give a connection to my SQL database and I want to use this connection on a button event in another form.How can I accomplish this? Please help me out.
Thanks in advance
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Krishna GaradRank #191 · 12.8K reputationPosted Dec 23, 2012, 7:34 PM EST
Class Connection
{
SqlConnection cn;
public void OpenConnection()
{
cn=new SqlConnection("Your Connection String Here...");
cn.open();
}
public void CloseConnection()
{
cn.Close();
}
}
and in your form call this methods like
button_click(...)
{
//create instance of connection class
Connection clscon=new Connection();
clscon.OpenConnection();
//Do your work here.......
//
//close connection after your work finish
clscon.CloseConnection();
}
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 23, 2012, 10:48 PM EST