473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reuse paramter list and reuse connection

I can't seem to find where to reset the parameter list.

Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

Now I want to use the same SqlCommand object and the same data connection to
do another select.

How do I clear the old parameters to allow me to readd the new ones and do I
need to do an objConn.close and another objConn.Open to do this?

Thanks,

Tom
Nov 19 '05 #1
5 1757
To clear the parameters, use this syntax:
objCmd.Paramete rs.Clear()

Yes, you should always close the connection as soon as you can after
executing a query, and don't open a connection until just before you execute
a query. The built-in ADO.NET connection pooling makes this very efficient
in almost all circumstances.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:e7******** ******@TK2MSFTN GP14.phx.gbl...
I can't seem to find where to reset the parameter list.

Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

Now I want to use the same SqlCommand object and the same data connection
to do another select.

How do I clear the old parameters to allow me to readd the new ones and do
I need to do an objConn.close and another objConn.Open to do this?

Thanks,

Tom

Nov 19 '05 #2
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uq******** *****@TK2MSFTNG P09.phx.gbl...
To clear the parameters, use this syntax:
objCmd.Paramete rs.Clear()
That was what I was looking for here.

Yes, you should always close the connection as soon as you can after
executing a query, and don't open a connection until just before you
execute a query. The built-in ADO.NET connection pooling makes this very
efficient in almost all circumstances.
What if I am doing a 3 or 4 selects in a row to fill dropdowns or a
datagrid - one right after another?

If I am doing a datareader, I have to wait until I am done reading it before
I can close it, so wouldn't be better to just do something like:

*************** *************** *************** *************** *************** *************** *
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

objCmd.Paramete rs.Clear()

objCmd.CommandT ext = "Select Counties,Countr yCode from Countries where
ClientCode = @ClientCode"
with objCmd.Paramete rs
.Add("@ClientCo de",SqlDbType.V arChar,10).valu e = ClientCode.Text
end with
objConn.Open()

Countries.DataS ource=objCmd.Ex ecuteReader
Countries.datab ind()

' I assume I would not need to do an objCmd.Paramete rs.Clear() here, since
I am using the same paramter and adding another (although my assumption may
be incorrect).

objCmd.CommandT ext = "Select CarrierCodes, CarrierNames from Carrierswhere
ClientCode = @ClientCode and State = @StateCode"
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = StateCode.Text
end with
objConn.Open()

Carriers.DataSo urce=objCmd.Exe cuteReader
Carriers.databi nd()
*************** *************** *************** *************** *************** **********'

Or should I close and open the connection between each select?

Thanks,

Tom


--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:e7******** ******@TK2MSFTN GP14.phx.gbl...
I can't seem to find where to reset the parameter list.

Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

Now I want to use the same SqlCommand object and the same data connection
to do another select.

How do I clear the old parameters to allow me to readd the new ones and
do I need to do an objConn.close and another objConn.Open to do this?

Thanks,

Tom


Nov 19 '05 #3
Well, if your code works then it works. Why fix what ain't broken?
Then it just becomes a matter of what is most efficient and how much effort
you are willing to put in to squeeze out every last bit of performance.
Technically, the performance would be best if you rolled all these queries
into a single stored procedure that returns multiple result sets.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:ed******** ******@TK2MSFTN GP10.phx.gbl...
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uq******** *****@TK2MSFTNG P09.phx.gbl...
To clear the parameters, use this syntax:
objCmd.Paramete rs.Clear()


That was what I was looking for here.

Yes, you should always close the connection as soon as you can after
executing a query, and don't open a connection until just before you
execute a query. The built-in ADO.NET connection pooling makes this very
efficient in almost all circumstances.


What if I am doing a 3 or 4 selects in a row to fill dropdowns or a
datagrid - one right after another?

If I am doing a datareader, I have to wait until I am done reading it
before I can close it, so wouldn't be better to just do something like:

*************** *************** *************** *************** *************** *************** *
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

objCmd.Paramete rs.Clear()

objCmd.CommandT ext = "Select Counties,Countr yCode from Countries where
ClientCode = @ClientCode"
with objCmd.Paramete rs
.Add("@ClientCo de",SqlDbType.V arChar,10).valu e = ClientCode.Text
end with
objConn.Open()

Countries.DataS ource=objCmd.Ex ecuteReader
Countries.datab ind()

' I assume I would not need to do an objCmd.Paramete rs.Clear() here,
since I am using the same paramter and adding another (although my
assumption may be incorrect).

objCmd.CommandT ext = "Select CarrierCodes, CarrierNames from Carrierswhere
ClientCode = @ClientCode and State = @StateCode"
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = StateCode.Text
end with
objConn.Open()

Carriers.DataSo urce=objCmd.Exe cuteReader
Carriers.databi nd()
*************** *************** *************** *************** *************** **********'

Or should I close and open the connection between each select?

Thanks,

Tom


--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:e7******** ******@TK2MSFTN GP14.phx.gbl...
I can't seem to find where to reset the parameter list.

Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

Now I want to use the same SqlCommand object and the same data
connection to do another select.

How do I clear the old parameters to allow me to readd the new ones and
do I need to do an objConn.close and another objConn.Open to do this?

Thanks,

Tom



Nov 19 '05 #4
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl...
Well, if your code works then it works. Why fix what ain't broken?
Then it just becomes a matter of what is most efficient and how much
effort you are willing to put in to squeeze out every last bit of
performance.
Technically, the performance would be best if you rolled all these queries
into a single stored procedure that returns multiple result sets.
I don't know if this works (I assume it does). I am just trying to find
other and better ways to do it.

As far as the multiple results sets go, I am not sure yet how to handle the
result sets when they get back.

Tom
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:ed******** ******@TK2MSFTN GP10.phx.gbl...
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uq******** *****@TK2MSFTNG P09.phx.gbl...
To clear the parameters, use this syntax:
objCmd.Paramete rs.Clear()


That was what I was looking for here.

Yes, you should always close the connection as soon as you can after
executing a query, and don't open a connection until just before you
execute a query. The built-in ADO.NET connection pooling makes this
very efficient in almost all circumstances.


What if I am doing a 3 or 4 selects in a row to fill dropdowns or a
datagrid - one right after another?

If I am doing a datareader, I have to wait until I am done reading it
before I can close it, so wouldn't be better to just do something like:

*************** *************** *************** *************** *************** *************** *
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

objCmd.Paramete rs.Clear()

objCmd.CommandT ext = "Select Counties,Countr yCode from Countries where
ClientCode = @ClientCode"
with objCmd.Paramete rs
.Add("@ClientCo de",SqlDbType.V arChar,10).valu e = ClientCode.Text
end with
objConn.Open()

Countries.DataS ource=objCmd.Ex ecuteReader
Countries.datab ind()

' I assume I would not need to do an objCmd.Paramete rs.Clear() here,
since I am using the same paramter and adding another (although my
assumption may be incorrect).

objCmd.CommandT ext = "Select CarrierCodes, CarrierNames from
Carrierswhere ClientCode = @ClientCode and State = @StateCode"
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = StateCode.Text
end with
objConn.Open()

Carriers.DataSo urce=objCmd.Exe cuteReader
Carriers.databi nd()
*************** *************** *************** *************** *************** **********'

Or should I close and open the connection between each select?

Thanks,

Tom


--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:e7******** ******@TK2MSFTN GP14.phx.gbl...
I can't seem to find where to reset the parameter list.

Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

Now I want to use the same SqlCommand object and the same data
connection to do another select.

How do I clear the old parameters to allow me to readd the new ones and
do I need to do an objConn.close and another objConn.Open to do this?

Thanks,

Tom



Nov 19 '05 #5
You can use the DataReader.Next Result method.
Here's more info:
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oj******** ******@TK2MSFTN GP14.phx.gbl...
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl...
Well, if your code works then it works. Why fix what ain't broken?
Then it just becomes a matter of what is most efficient and how much
effort you are willing to put in to squeeze out every last bit of
performance.
Technically, the performance would be best if you rolled all these
queries into a single stored procedure that returns multiple result sets.


I don't know if this works (I assume it does). I am just trying to find
other and better ways to do it.

As far as the multiple results sets go, I am not sure yet how to handle
the result sets when they get back.

Tom

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:ed******** ******@TK2MSFTN GP10.phx.gbl...
"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:uq******** *****@TK2MSFTNG P09.phx.gbl...
To clear the parameters, use this syntax:
objCmd.Paramete rs.Clear()

That was what I was looking for here.
Yes, you should always close the connection as soon as you can after
executing a query, and don't open a connection until just before you
execute a query. The built-in ADO.NET connection pooling makes this
very efficient in almost all circumstances.

What if I am doing a 3 or 4 selects in a row to fill dropdowns or a
datagrid - one right after another?

If I am doing a datareader, I have to wait until I am done reading it
before I can close it, so wouldn't be better to just do something like:

*************** *************** *************** *************** *************** *************** *
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
.Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
end with
objConn.Open()

ZipCode.DataSou rce=objCmd.Exec uteReader
ZipCode.DataTex tField= "ZipCode"
ZipCode.DataVal ueField="ZipCod e"
ZipCode.databin d()

objCmd.Paramete rs.Clear()

objCmd.CommandT ext = "Select Counties,Countr yCode from Countries where
ClientCode = @ClientCode"
with objCmd.Paramete rs
.Add("@ClientCo de",SqlDbType.V arChar,10).valu e = ClientCode.Text
end with
objConn.Open()

Countries.DataS ource=objCmd.Ex ecuteReader
Countries.datab ind()

' I assume I would not need to do an objCmd.Paramete rs.Clear() here,
since I am using the same paramter and adding another (although my
assumption may be incorrect).

objCmd.CommandT ext = "Select CarrierCodes, CarrierNames from
Carrierswhere ClientCode = @ClientCode and State = @StateCode"
with objCmd.Paramete rs
.Add("@StateCod e",SqlDbType.Ch ar,2).value = StateCode.Text
end with
objConn.Open()

Carriers.DataSo urce=objCmd.Exe cuteReader
Carriers.databi nd()
*************** *************** *************** *************** *************** **********'

Or should I close and open the connection between each select?

Thanks,

Tom

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"tshad" <ts**********@f tsolutions.com> wrote in message
news:e7******** ******@TK2MSFTN GP14.phx.gbl...
>I can't seem to find where to reset the parameter list.
>
> Dim objCmd as New SqlCommand(Comm andText,objConn )
> with objCmd.Paramete rs
> .Add("@StateCod e",SqlDbType.Ch ar,2).value = ByState.Selecte dValue
> .Add("@City",Sq lDbType.char).v alue = ByCity.Selected Value
> end with
> objConn.Open()
>
> ZipCode.DataSou rce=objCmd.Exec uteReader
> ZipCode.DataTex tField= "ZipCode"
> ZipCode.DataVal ueField="ZipCod e"
> ZipCode.databin d()
>
> Now I want to use the same SqlCommand object and the same data
> connection to do another select.
>
> How do I clear the old parameters to allow me to readd the new ones
> and do I need to do an objConn.close and another objConn.Open to do
> this?
>
> Thanks,
>
> Tom
>



Nov 19 '05 #6

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

Similar topics

3
6883
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By composition mainly? What're others? Thanks in advance for your comments!
0
1477
by: anders thoresson | last post by:
Hi, I have this function I call everytime I need to make a query from within my php-scripts: function db_connect ($user, $pwd, $db, $debug = 0) { $link = @mysql_pconnect("localhost", "$user", "$pwd"); if($link && mysql_select_db("$db")) if($debug == 1) {
3
1277
by: danra | last post by:
Hi, I have a question which seems to me pretty basic, unfortunately I can't seem to figure it out. Let's say I have an abstract base class Vehicle and classes Car and Truck which derive from it. Let's say I have abstract base class A which contains a list of vehicles, and I define a method to insert and handle a vehicle into
34
10753
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
0
4058
by: integragreg | last post by:
I apologize in advance if I am posting to the wrong group, but at least one of my questions is related to Platform Invoke in C#. I am using .NET Framework 1.1, and for improved performance, I need to be able to reuse a client socket by placing it into a pool. Unfortunately, with .NET Framework 1.1 I have no way to close a socket's...
4
3619
by: Craig Buchanan | last post by:
I would like to reuse a SQLDataReader that is populated with information as the datasource for multiple dropdownlists. Unfortunately, the first DDL closes the SDR and my code fails on the second DDR. Is there a way to copy/clone the SDR? Is there another solution? Thanks, Craig Buchanan
3
3959
by: steph | last post by:
Hi, I've got below function to dynamically define and run pass-through queries. This works OK. But I don't want to have the connection string hard-coded, instead I want to reuse the existing connection. How can I achieve this? Thanks, Stephan
2
1198
by: Garx | last post by:
Hi Guys, This is my first post so bear with me :) I am running Access 2003 and am still feeling my way around it. I have a form (FORM_IDL) that uses a combobox (cboIDA) which uses a query as its source. Now when the user enters an IDA value thats not in the list of the combobox I want the user to be able to choose whether they add the...
7
1437
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line. I have the following section of code: if (ev.locationList != null) { //isListNull = true ensures that we do not recheck the list every time we add a new item bool isListNull = false;
0
7694
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...
0
7609
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...
0
7921
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. ...
1
7666
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...
0
6278
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...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
1208
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.