473,623 Members | 3,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=north wind");

//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.Fil l(thisDataSet, "Customers" );

foreach (DataRow theRow in thisDataSet.Tab les["Customers"].Rows)
Console.WriteLi ne("Row = {0}", theRow["CustomerID "] + "\t" +
theRow["ContactNam e"]);

Console.WriteLi ne("Program finished, press Enter/Return to
continue");
Console.Read();
}
Aug 25 '08 #1
1 1597
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2589
by: Lasse Edsvik | last post by:
Hello Im trying to create a simple testclass that connects to a db on localhost and a method that returns a dataset. I get these errors: Unhandled Exception: System.InvalidOperationException: Fill: SelectCommand.Conne ction property has not been initialized. at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter, IDb
5
2626
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will bear with me. I am going through the QuickStart. After reading a little, I am trying to implement a simple page on a simple project I have made up. I have a page called default.aspx. I want it to load a list of user names from a SQL database...
3
13968
by: Nils Magnus Englund | last post by:
Hi, I've made a HttpModule which deals with user authentication. On the first request in a users session, it fetches data from a SQL Server using the following code: using (SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings)) {
1
1072
by: Wizard | last post by:
Hey Group, Im having some major trouble trying to find info on this one:( Wonder if somebody could possibly help? At Present I can get this working: Private Sub frmSystemOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
4
1266
by: R.A.M. | last post by:
Hello, (Sorry for my English...) I am learning C# and I have a question: is it good practice to use "using", for example: using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); ... connection.Close();
0
6204
by: coosa | last post by:
Dear all; My code is is a bit long but is modular at least. I'm attempting to implement the depth first search for an application that is supposed to: 1- Fetch based on an ID from the database a list of other IDs. 2- Those IDs fetched should be iterated one by one and step 1 is repeated again until no more ID have any more fetched sub lists The implementation always resulted in an empty object even though I could trace during Debugging...
22
60223
Frinavale
by: Frinavale | last post by:
How To Use A Database In Your Program Many .NET solutions are database driven and so many of us often wonder how to access the database. To help you understand the answer to this question I've provided the following as a quick example of how to retrieve data from a database. In order to connect to a SQL Server Database using .NET you will need to import the System.Data.SqlClient package into your program. If you are not connecting to a...
11
1561
by: James R. Davis | last post by:
Yes, a newbie here. Though I am making progress, slowly, I am also getting more and more confused. With ASP, when I wanted to do something as trivial as updating a visitor counter, I connected to a database, executed a SQL command to read the current value of a field into a recordset, updated the value by adding 1 and writing the field back to the table, closed and got rid of the connection and recordset. I had no concerns about how...
9
2909
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading the text files from the watched folder line by line into variables and then posting them to a SQL table. All of the code for the form is shown below. And it works fine except for 2 issues. First issue: In the Finally of the Try for teh SQL...
0
8162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8662
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6104
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4067
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.