471,347 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

Simple program without using the open for SqlConnection

Hello!

Below is a simple program that works fine.
In this program there is no explicit open for the SqlConnection instead
the DataAdapter will open the Connection to the database automatically.

Now to my question if I check the connection after the SqlDataAdapter has
been created the connection is then closed ?

So when does the SqlDataAdapter open and close the DataAdapter ?

static void Main(string[] args)
{
//Specify SQL Server-specific connection string
SqlConnection thisConnection = new SqlConnection(
@"Server=UHT-DEMO1; Integrated Security=True;" +
"Database=northwind");

//Create DataAdapter object
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"Select CustomerID, ContactName from Customers,
thisConnection);

ConnectionState state1 = thisConnection.State;

//Create DataSet to contain related data tables, rows and
columns
DataSet thisDataSet = new DataSet();

//Fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Customers");

foreach (DataRow theRow in thisDataSet.Tables["Customers"].Rows)
Console.WriteLine("Row = {0}", theRow["CustomerID"] + "\t" +
theRow["ContactName"]);

Console.WriteLine("Program finished, press Enter/Return to
continue");
Console.Read();
}
Aug 25 '08 #1
1 1503
As I understand it, if a SqlDataAdapter is given a non-open
connection, it will open it just before it needs it (Fill) and close
it when it has finished (before exiting Fill). If you give it an open
connection it leaves it alone. Note that I'm not really a big
DataAdapter user, so this is just from rough memory...

Marc
Aug 25 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lasse Edsvik | last post: by
5 posts views Thread by Stephanie_Stowe | last post: by
3 posts views Thread by Nils Magnus Englund | last post: by
1 post views Thread by Wizard | last post: by
4 posts views Thread by R.A.M. | last post: by
11 posts views Thread by James R. Davis | last post: by
9 posts views Thread by Keith G Hicks | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.