473,731 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlConnection, SqlCommand, SqlDataReader

Hi,

In C#.Net, can we have more than one SqlCommand in a SqlConnection?
And in one SqlCommand, can we have more than one SqlDataReader in a
SqlCommand?
In what situation should I use SqlConneciton.D ispose() instead of
SqlConnection.C lose()?
Thanks for help.
Jason
Jan 20 '06 #1
3 3856
Hello Jason,
Hi,

In C#.Net, can we have more than one SqlCommand in a SqlConnection?
And in one SqlCommand, can we have more than one SqlDataReader in a
SqlCommand?
You assume that there's a one-to-one relationship between these types, which
isn't the case. You can create as many SqlCommands as you want and run them
over the same SqlConnection. Also, there are no SqlDataReaders "in" a command.
They are results of queries performed by a particular SqlCommand.
In what situation should I use SqlConneciton.D ispose() instead of
SqlConnection.C lose()?


These methods do the same. Close() is just a more fitting and intuitive name
than Dispose() for types like connections, files, or streams.

Cheers,
--
Joerg Jooss
ne********@joer gjooss.de
Jan 21 '06 #2
Thanks Joerg.
In my code,
SqlCommand myCommand = new SqlCommand(strS QL,Conn);
SqlCommand myCommand2 = new SqlCommand(strS QL,Conn);
myReader = myCommand.Execu teReader();
myReader2 = myCommand2.Exec uteReader();

When the program come to myReader2=myCom mand2.ExecuteRe ader(), it will incur
an error.
So, I was asking if in one SqlConnection can has more than one SqlCommand.


"Joerg Jooss" <ne********@joe rgjooss.de> ¼¶¼g©ó¶l¥ó·s»D: 94************* ************@ms news.microsoft. com...
Hello Jason,
Hi,

In C#.Net, can we have more than one SqlCommand in a SqlConnection?
And in one SqlCommand, can we have more than one SqlDataReader in a
SqlCommand?


You assume that there's a one-to-one relationship between these types,
which isn't the case. You can create as many SqlCommands as you want and
run them over the same SqlConnection. Also, there are no SqlDataReaders
"in" a command. They are results of queries performed by a particular
SqlCommand.
In what situation should I use SqlConneciton.D ispose() instead of
SqlConnection.C lose()?


These methods do the same. Close() is just a more fitting and intuitive
name than Dispose() for types like connections, files, or streams.

Cheers,
--
Joerg Jooss
ne********@joer gjooss.de

Jan 22 '06 #3
Hello Jason,
Thanks Joerg.
In my code,
SqlCommand myCommand = new SqlCommand(strS QL,Conn);
SqlCommand myCommand2 = new SqlCommand(strS QL,Conn);
myReader = myCommand.Execu teReader();
myReader2 = myCommand2.Exec uteReader();
When the program come to myReader2=myCom mand2.ExecuteRe ader(), it will
incur
an error.
So, I was asking if in one SqlConnection can has more than one
SqlCommand.


As usual, seeing the code opens my eyes ;-)

This can only be done with ADO.NET 2.0 using a new SQL Server 2005 feature
called MARS (Multiple Active Result Sets).

Cheers,
--
Joerg Jooss
ne********@joer gjooss.de
Jan 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
558
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the SqlCommand(). At that point I cannot use SqlConnection() again. Unless I .Close() it and then .Open it right? Then what is 'connection pooling' ? Can't I run multiple SqlCommand()s on the same SqlConnection() somehow ?
1
4936
by: huzz | last post by:
I have a method that gets sqldatareader as shown below.. my question is how do i close the sqlconnection (objConn) object within this method? If i put the objconn.Close(); after the return then i get "Unreachable code detected" and if i close it before the return i get "Invalid attempt to FieldCount when reader is closed." When i refresh the page .. lot of sqlconnection is created.. and then i get this error message : Timeout expired....
1
2390
by: Mark | last post by:
I have an SqlConnection object defined as a member of main form. I also have a thread that creates an SqlDataReader but uses the same SqlConnection object. The SqlDataReader simple loops round for each row in the result set. The problem I have is that after running the thread many times, the SqlConnection object simply closes the connection to the database (picked up from the StateChange event). No exceptions are generated. Sometimes the...
3
1661
by: sck10 | last post by:
Hello, I have a stored procedure that requires 3 parameters. How do you add multiple parameters for a SqlConnection? Thanks, Steven Public strConn = "Data Source=MyServer;Initial Catalog=MyDatabase;User Id=MyUser;Password=MyPwd;"
3
1795
by: Ludvig | last post by:
This Exception is executed when using SQLConnection. OleDbConnection connects fine. System.EnterpriseServices.Platform.Initialize() +503 System.EnterpriseServices.ResourcePool..ctor(TransactionEndDelegate cb) +11 System.Data.SqlClient.ConnectionPool..ctor(DefaultPoolControl ctrl) +797 System.Data.SqlClient.PoolManager.FindOrCreatePool(DefaultPoolControl ctrl) +170
2
2396
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)) {
0
1003
by: wk6pack | last post by:
Hi, I have a program that goes against SQL Server. I have opened a connection and closed all the connections with .close() and ..dispose() but after I read in 32 records, it says it maxed out on the connections. Is there something I'm doing wrong? Should I pass in the connection as a reference instead of opening and
2
5453
by: axel22 | last post by:
Ok here it goes. 1. I've created a new web service project in F:\c#\WebSites\ServisBaze. 2. I've published the web service with IIS in a virtual directory who's actual path is: F:\c#\Published\ServisBaze 3. Tried opening it in a browser - it works perfectly. 4. Now I've added an SQL Database object Database.mdf into my project. 5. I've manually edited the database tables and added data. 6. I've added a webmethod 'izvadiImena' to work...
8
14413
by: JoeW | last post by:
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my queries but recently a friend of mine who is a long time java programmer suggested I use the IDbConnection, IDbCommand interfaces because the queries where compiled instead of just executed like on the open SqlConnection. Is he correct and if so does it really make a difference as far as performance/security, etc? Thanks for any suggestions
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9443
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...
0
9303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9233
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
6732
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
4803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3258
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
2
2714
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.