471,338 Members | 959 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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.Dispose() instead of
SqlConnection.Close()?
Thanks for help.
Jason
Jan 20 '06 #1
3 3734
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.Dispose() instead of
SqlConnection.Close()?


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********@joergjooss.de
Jan 21 '06 #2
Thanks Joerg.
In my code,
SqlCommand myCommand = new SqlCommand(strSQL,Conn);
SqlCommand myCommand2 = new SqlCommand(strSQL,Conn);
myReader = myCommand.ExecuteReader();
myReader2 = myCommand2.ExecuteReader();

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


"Joerg Jooss" <ne********@joergjooss.de> glsD:94*************************@msnews. 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.Dispose() instead of
SqlConnection.Close()?


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********@joergjooss.de

Jan 22 '06 #3
Hello Jason,
Thanks Joerg.
In my code,
SqlCommand myCommand = new SqlCommand(strSQL,Conn);
SqlCommand myCommand2 = new SqlCommand(strSQL,Conn);
myReader = myCommand.ExecuteReader();
myReader2 = myCommand2.ExecuteReader();
When the program come to myReader2=myCommand2.ExecuteReader(), 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********@joergjooss.de
Jan 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Donnie Darko | last post: by
1 post views Thread by huzz | last post: by
1 post views Thread by Mark | last post: by
3 posts views Thread by sck10 | last post: by
3 posts views Thread by Ludvig | last post: by
2 posts views Thread by Nils Magnus Englund | last post: by
reply views Thread by wk6pack | last post: by
2 posts views Thread by axel22 | last post: by
8 posts views Thread by JoeW | last post: by
reply views Thread by rosydwin | 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.