0

I have this c# application where the user needs to login through the first form. If validation returns true form2 opens up .

I want to maintain a single connection through all these forms with the same user credentials as entered in form1.I am using ODP.NET to connect to oracle 11g.

Thanks.

Pdksock
  • 963
  • 2
  • 9
  • 26
  • @otiel i connect to the db from form1 but i am unable to use the same connection to the db in form2.I need to reconnect.Is there any way in c# to make this happen? – Pdksock Sep 20 '12 at 06:14
  • As pointed by *Niyoko Yuliawan* in his answer, you should close your SqlConnection as soon as you finished your query. Thus, you don't have to use a single connection through all your forms. – Otiel Sep 20 '12 at 07:25

2 Answers2

2

Sql connection in .NET is managed by connection pool. So if you instantiate new connection objects, it reuses old closed physical connection.

in form1

using(var c = new SqlConnection("connectionstring"))
{
     //use connection here
}

in form2

using(var c = new SqlConnection("connectionstring"))
{
    //use connecion here
}

form1 and form2 use same physical connection to database

connection pooling also available for Oracle Data Provider

or maybe you interested in Entity Framework

Community
  • 1
  • 1
Niyoko
  • 6,957
  • 4
  • 29
  • 57
0

Create a Static Connectionstring class like if you declare a Static class with properties of Sqlconnection you can access it in any form or any other class directly here is a sample of class

public static class Connection
    {
        private static SqlConnection sqlconn;
        public static SqlConnection getconnection() {
            if (sqlconn==null)
               sqlconn = new SqlConnection("Connectionsting.");
            return sqlconn;
        }


    }
Anant Dabhi
  • 9,506
  • 2
  • 28
  • 48