Ron

Ron

  • NA
  • 44
  • 64.4k

Cleaning up an ADO connection.

Aug 19 2010 3:50 PM

I have written a class that accesses data from an SQL database. Here is the class:

public DataSet GetData(int nCol)
string cSQLSelect = @"SELECT * FROM table";
SqlConnection conDB = new SqlConnection(GetConnString());
SqlDataAdapter da = new SqlDataAdapter(cSQLSelect, conDB);
DataSet ds = new DataSet();
da.Fill(ds,
"table");
return ds;

In my windows form I'm calling this class and binding the first column to a textbox. Here is the code:

private void button1_Click(object sender, EventArgs e)
{
DataSet ds = class.GetData(11007);
textBox1.DataBindings.Add(
"text", ds, "table.col1");
}

This code works, the data appears in the textbox just as I expected. But here is my concern: Am I leaving the connection open by using this methodology? Apparently the data adapter does not require a call to the open method. So does the connection close automatically?

I'm new to C# programming, and I don't know the best strategy to handle this situation.
 

Answers (4)