Question

My form calls multiple TableAdapter objects to fill a local, common DataSet with relevant rows (see below).

private void frm_TrialsFromOrder_Load(object sender, EventArgs e)
{
    this.trialTableAdapter.FillBy(this.myDataSet.trialTable, this.trial_id);
    this.orderTableAdapter.FillBy(this.myDataSet.orderTable, this.order_id);
}

When executing the program, the SQL Server Profiler shows:

  1. Audit Logon
  2. SQL:Batch (SELECT ... FROM trialTable ...)
  3. Audit Logoff
  4. Audit Logon
  5. SQL:Batch (SELECT ... FROM orderTable ...)
  6. Audit Logoff

How can I configure multiple TableAdapter objects to use the same SqlConnection?

Was it helpful?

Solution

You don't want to. The SqlConnection object is built to be created, inside of a using as a best practice, used, and then disposed (hence the using statement). Do not share connection objects. The operation of pooling connections to ensure that it's not expensive is handled by the server.

Per MSDN:

To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.

Do make sure you're setting the Pooling property on the connection string to true so that pooling is enabled; that would defeat the purpose otherwise (in general).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top