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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Dec 24, 2012, 3:48 AM
Krishna GaradPosted Dec 24, 2012, 12:34 AM
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();
}