use of connections to database
Posted on 2006-11-08
I have a page where I am doing 2 seperate queries to an access database. My question is should I close the connection after the first query and the re-open it.....or do I just leave it open, then close at the end.
What is the best and efficient way to do this?
// Connect to the Access database using a query to get all the data from the table.
OleDbDataAdapter myAdapter = new OleDbDataAdapter("SELECT * FROM table1);
// Create and fill a DataSet.
DataSet ds = new DataSet();
connection.Close(); ////////////////////the magic line, should I have this here or not?
// Bind MyRepeater to the DataSet.
MyRepeater.DataSource = ds;
//Use a variable to hold the SQL statement.
string selectString = "SELECT * FROM [table2]";
//Open the connection.
//Create an OleDbCommand object.
OleDbCommand cmd = new OleDbCommand(selectString,connection);
OleDbDataReader reader = cmd.ExecuteReader();
this.field1.Text = reader["field1"].ToString();
this.field2.Text = reader["field2"].ToString();
//Close the reader and the related connection.
I look forward in hearing thoughts on this.