473,668 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlCommand

Tom
Hello guys
Please have a look on following paragraph from ".NET Data
Access Architecture Guide".
'''''''''''
Although you can repeatedly use the same SqlCommand object
to execute the same command multiple times, do not reuse
the same SqlCommand object to execute different commands.
1) You do not need to explicitly open or close the
database connection. The SqlDataAdapter Fill method opens
the database connection and then closes the connection
before it returns. If the connection is already open, Fill
leaves the connection open.
2) If you require the connection for other purposes,
consider opening it prior to calling the Fill method. You
can thus avoid unnecessary open/close operations and gain
a performance benefit.
3) Although you can repeatedly use the same SqlCommand
object to execute the same command multiple times, do not
reuse the same SqlCommand object to execute different
commands. (????)
'''''''''''
Now can anybody explain reasons behind last one?
Check the two functions I attach below.
I use the first style often (but never use the second one,
I always use different data adapters to fill data into
dataset from different tables)
Please tell me that where should I apply the third rule.
Private Sub UsingSameComman dToFillReader()
Dim mySqlConnection As SqlClient.SqlCo nnection
Dim mySqlCommand As SqlClient.SqlCo mmand
Dim mySqlDataReader As SqlClient.SqlDa taReader

mySqlConnection = New SqlClient.SqlCo nnection("My
Connection String")
mySqlConnection .Open()
mySqlCommand = New SqlClient.SqlCo mmand()
mySqlCommand.Co nnection = mySqlConnection

'First Call
mySqlCommand.Co mmandType = CommandType.Sto redProcedure
mySqlCommand.Co mmandText = "spFirstProcedu re"
mySqlCommand.Pa rameters.Add("@ ID", 1)
mySqlDataReader = mySqlCommand.Ex ecuteReader()
Do While mySqlDataReader .Read
'---- Get Values
Loop
mySqlDataReader .Close()

'Second Call
mySqlCommand.Pa rameters.Clear( )
mySqlCommand.Co mmandType = CommandType.Sto redProcedure
mySqlCommand.Co mmandText = "spSecondProced ure"
mySqlCommand.Pa rameters.Add("@ Name", "Test")
mySqlDataReader = mySqlCommand.Ex ecuteReader()
Do While mySqlDataReader .Read
'---- Get Values
Loop
mySqlDataReader .Close()

'Close the Connection
mySqlConnection .Close()
End Sub

Private Sub UsingSameComman dToFillDataSet( )
'Please note that I just want to fill the dataset, no
update required.
Dim mySqlConnection As SqlClient.SqlCo nnection
Dim mySqlCommand As SqlClient.SqlCo mmand
Dim mySqlDataAdapte r As SqlClient.SqlDa taAdapter
Dim myDataSet As DataSet

mySqlConnection = New SqlClient.SqlCo nnection("My
Connection String")
mySqlConnection .Open()
mySqlCommand = New SqlClient.SqlCo mmand()
mySqlCommand.Co nnection = mySqlConnection

'First Call
mySqlCommand.Co mmandType = CommandType.Sto redProcedure
mySqlCommand.Co mmandText = "spFirstProcedu re"
mySqlCommand.Pa rameters.Add("@ ID", 1)

mySqlDataAdapte r.SelectCommand = mySqlCommand
mySqlDataAdapte r.Fill(myDataSe t)
'Can I uncomment follwing line to remove the reference of
sqlcommand from data adapter
'mySqlDataAdapt er.SelectComman d = Nothing

'Second Call
mySqlCommand.Pa rameters.Clear( )
mySqlCommand.Co mmandType = CommandType.Sto redProcedure
mySqlCommand.Co mmandText = "spSecondProced ure"
mySqlCommand.Pa rameters.Add("@ Name", "Test")

mySqlDataAdapte r.SelectCommand = mySqlCommand
mySqlDataAdapte r.Fill(myDataSe t)

'Close the Connection
mySqlConnection .Close()
End Sub
Regards
Tom

Nov 20 '05 #1
1 5591
Cor
Hi Tom

First of all Tom, it maybe depends if you put things global or create them
in the routines (in the form or separate classes) every time.

But some comments.
Although you can repeatedly use the same SqlCommand object
to execute the same command multiple times, do not reuse
the same SqlCommand object to execute different commands.
For what is this method than
cmd.commandtext = "xxxxxxxxx"
1) You do not need to explicitly open or close the
database connection. The SqlDataAdapter Fill method opens
the database connection and then closes the connection
before it returns. If the connection is already open, Fill
leaves the connection open.
That is so easy to test for yourself, just put a conn.close before the Fill.
(I did not get an error doing that)
2) If you require the connection for other purposes,
consider opening it prior to calling the Fill method. You
can thus avoid unnecessary open/close operations and gain
a performance benefit.
I always keep the Internet user in mind, sometimes he just closes his
browser and plug of his computer.
3) Although you can repeatedly use the same SqlCommand
object to execute the same command multiple times, do not
reuse the same SqlCommand object to execute different
commands. (????)


I don't understand it maybe either, but see my comment with 1.

Just some thoughts.

Cor
Nov 20 '05 #2

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 ?
10
18500
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually release? I would basically prefer to skip invoking Dispose() as this will free me from determining when the usage actually has finished.
3
3850
by: Jason Huang | last post by:
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.
0
1165
by: Agnes | last post by:
I got a silly question about Sqlcommand . In my form, there are 5 methods which use the same sqlcommand to execute reader. I had create and dispose for each sqlcommand in every method. Some said that I can only delcare one sqlcommand and re-use it in every method and no need to dispose until form closed. I am wonder which approach is correct. public class myform Dim cmdSearch As New SqlClient.SqlCommand method 1
10
3938
by: John Bailo | last post by:
I want to pass a SqlCommand object as a input parameter to a method. I want to pass the SqlCommand "by value" so that any updates to the original object are *not* reflected in the object within my method. How can I do this?
1
7249
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
0
2491
by: =?Utf-8?B?ZGF2ZQ==?= | last post by:
The following finishes in 1 hour instead of erroring after 1 second as expected. Dim cn As New System.Data.SqlClient.SqlConnection cn.ConnectionString = "Server=(local); Database=master; User ID=sa; Password=passwd; Asynchronous Processing=true;" cn.Open() Dim sqlcommand As New System.Data.SqlClient.SqlCommand sqlcommand.Connection = cn
2
2102
by: TheSteph | last post by:
Hi I have a SQLCommand that do updates on a table. I have a loop where I set the parameters then I call ExecuteNonQuery. For . { . Set the SQLCommand's Params Values;
6
2950
by: Frank Hauptlorenz | last post by:
Hello, I'm trying to send an SqlCommand to a WCF-Service. For this I'm using the following DataContract: public class SqlCommandComposite { SqlCommand cmd = new SqlCommand();
0
8381
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
8893
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
8797
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
8583
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,...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
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...
1
2791
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
1786
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.