I need help with database connections and my winapp.
I have a windows app (C#) that, after I log in, starts running 5 or 6 different queries in Oracle database, every 5-10 seconds. Application is up 24/7.
What is a proper way to do this?
Should I open connection during login and never close it until I close the app, or should I open and close connection every time I run a query?
For example:
//first query
conn.Open();
DataSet ds1 = new DataSet();
string sql = "SELECT * FROM table1";
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds1, "Result1");
conn.Dispose();
return ds1;
//second query
conn.Open();
DataSet ds2 = new DataSet();
string sql = "SELECT * FROM table2";
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds2, "Result2");
conn.Dispose();
return ds2;
What’s the best way to do this?
The Oracle OleDB provider you use in your application implements pooling. So, when you create and clore a connection, you only take/release a connection from a pool of ‘real connection’.
This makes creating and disposing Connection objects a really cheap operation. If I were you, I would open a connection, execute my batch of queries, and close+dispose that connection as soon as I’m done and going to sleep (even for a few seconds).