Skip to content

Share a SQL DB connection in the same form

M T

Asked Apr 11, 2008, 8:27 AM EDT2 answers
Share
LinkedInX

Hi

I have been working in VB.NEt and recently shifted to C#. I want to open a DB connection and keep it open till user close the form. I want to use this connection for fetching different  record sets.

In VB, I can declare a CONN variable in teh beginig of the form and open the connection at the form load and keep on using it.

Is there a way in C#?

 

Thanks

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    using(...Connection oConnection = new ...Connection("ConnectionString"))
    {
        /* put your  code here, and after excution of the bloc  the  connection  will  be garbage  collected */ authomatically
    }
    0
  • Bechir Bejaoui
    0
    or impelent the closing event of the form by connection.Close();

    0