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

Home Posts Topics Members FAQ

Question on SQLCommand in a loop

I am using .Net 2003 and querying a SQL Server 2000 database.
I read the database in a loop, but on every iteration I set the SQLCommand
to new, set it, execute it, dispose and set it to nothing.
Is there any way so that I do not have to do that ? In VB6, I can set a
recordset to new once, set it and execute it within the loop, and close it
after the loop.
Thanks.

Dim m_cmdSQLT As SqlClient.SqlCo mmand
Dim m_drSQLTop As SqlClient.SqlDa taReader

Do While bContinue
sSQL = "GetPacketDataT op"
m_cmdSQLT = New SqlClient.SqlCo mmand
With m_cmdSQLT
.Connection = TickServerAdoCo n
.CommandType = CommandType.Sto redProcedure
.CommandText = sSQL
End With
m_cmdSQLT.Param eters.Add("@sCo ntract", SqlDbType.VarCh ar, 8)
m_cmdSQLT.Param eters.Add("@dTi me1", SqlDbType.DateT ime, 8)
m_cmdSQLT.Param eters.Add("@dTi me2", SqlDbType.DateT ime, 8)
m_cmdSQLT.Param eters("@sContra ct").Value = txtSymbol.Text
m_cmdSQLT.Param eters("@dTime1" ).Value = dTime1
m_cmdSQLT.Param eters("@dTime2" ).Value = dtime2
m_drSQLTop = m_cmdSQLT.Execu teReader()
If m_drSQLTop.Read Then sOpen = "" & m_drSQLTop("PAC KET_DATA")
m_drSQLTop.Clos e()
m_cmdSQLT.Dispo se()
m_cmdSQLT = Nothing
:
dTime1 = DateAdd(DateInt erval.Minute, 1, dTime1)
dtime2 = DateAdd(DateInt erval.Minute, 1, dTime1)
If DateDiff(DateIn terval.Minute, dLast, dTime1) >= 1 Then
bContinue = False
End If
Loop

Mar 8 '07 #1
2 5178
On Mar 8, 1:44 pm, "fniles" <fni...@pfmail. comwrote:
I am using .Net 2003 and querying a SQL Server 2000 database.
I read the database in a loop, but on every iteration I set the SQLCommand
to new, set it, execute it, dispose and set it to nothing.
Is there any way so that I do not have to do that ? In VB6, I can set a
recordset to new once, set it and execute it within the loop, and close it
after the loop.
Thanks.
You'll want to put the initialization of the Command object and the
parameter declarations outside the loop so the Command isn't created
and destroyed on each iteration.
Inside the loop, you'll assign the parameter values, execute the
command, and perform your logic on the values you get from the
DataReader.

When the loop ends, you can Dispose the command and close the
connection. Might look something like:

m_cmdSQLT = New SqlClient.SqlCo mmand
sSQL = "GetPacketDataT op"
With m_cmdSQLT
.Connection = TickServerADOCo n
.CommandType = CommandType.Sto redProcedure
.CommandText = sSQL
End With

m_cmdSQLT.Param eters.Add("@sCo ntract", SqlDbType.VarCh ar, 8)
m_cmdSQLT.Param eters.Add("@dTi me1", SqlDbType.DateT ime, 8)
m_cmdSQLT.Param eters.Add("@dTi me2", SqlDbType.DateT ime, 8)

Do While bContinue
m_cmdSQLT.Param eters("@sContra ct").Value = txtSymbol,text
m_cmdSQLT.Param eters("@dTime1" ).Value = dTime1
m_cmdSQLT.Param eters("@dTime2" ).Value = dTime2
m_drSQLTop = m_cmdSQLT.Execu teReader()
If m_drSQLTop.Read Then sOpen = "" &
m_drSQLTop("PAC KET_DATA")
m_drSQLTop.Clos e()

dTime1 = DateAdd(DateInt erval.Minute, 1, dTime1)
dTime2 = DateAdd(DateInt erval.Minute, 1, dTime1)
If DateDiff(DateIn terval.Minute, dLast, dTime1) >= 1 Then
bContinue = False
End If
Loop

' Close connection etc. here

Mar 8 '07 #2
Thanks, that works !

"jayeldee" <ja******@gmail .comwrote in message
news:11******** *************@h 3g2000cwc.googl egroups.com...
On Mar 8, 1:44 pm, "fniles" <fni...@pfmail. comwrote:
>I am using .Net 2003 and querying a SQL Server 2000 database.
I read the database in a loop, but on every iteration I set the
SQLCommand
to new, set it, execute it, dispose and set it to nothing.
Is there any way so that I do not have to do that ? In VB6, I can set a
recordset to new once, set it and execute it within the loop, and close
it
after the loop.
Thanks.

You'll want to put the initialization of the Command object and the
parameter declarations outside the loop so the Command isn't created
and destroyed on each iteration.
Inside the loop, you'll assign the parameter values, execute the
command, and perform your logic on the values you get from the
DataReader.

When the loop ends, you can Dispose the command and close the
connection. Might look something like:

m_cmdSQLT = New SqlClient.SqlCo mmand
sSQL = "GetPacketDataT op"
With m_cmdSQLT
.Connection = TickServerADOCo n
.CommandType = CommandType.Sto redProcedure
.CommandText = sSQL
End With

m_cmdSQLT.Param eters.Add("@sCo ntract", SqlDbType.VarCh ar, 8)
m_cmdSQLT.Param eters.Add("@dTi me1", SqlDbType.DateT ime, 8)
m_cmdSQLT.Param eters.Add("@dTi me2", SqlDbType.DateT ime, 8)

Do While bContinue
m_cmdSQLT.Param eters("@sContra ct").Value = txtSymbol,text
m_cmdSQLT.Param eters("@dTime1" ).Value = dTime1
m_cmdSQLT.Param eters("@dTime2" ).Value = dTime2
m_drSQLTop = m_cmdSQLT.Execu teReader()
If m_drSQLTop.Read Then sOpen = "" &
m_drSQLTop("PAC KET_DATA")
m_drSQLTop.Clos e()

dTime1 = DateAdd(DateInt erval.Minute, 1, dTime1)
dTime2 = DateAdd(DateInt erval.Minute, 1, dTime1)
If DateDiff(DateIn terval.Minute, dLast, dTime1) >= 1 Then
bContinue = False
End If
Loop

' Close connection etc. here

Mar 8 '07 #3

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

Similar topics

2
7210
by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a passed-in SqlCommand object. I can find the properties OK but am unsuccessful in trying to change their value due to the way I have identified those SqlCommand objects (as a PropertyInfo). Can anyone give me a hint (or suggest a better way) as...
2
8318
by: Andre Ranieri | last post by:
I'm having trouble with what should be a simple task; beginning and committing T-SQL transactions using the SQLClient. I'm using a SqlCommand (cmd) to begin the transaction and delete records from two tables. if both records are successfully deleted, I'd like to commit the transaction. Everything works fine until I attempt to commit the first time the foreach loop runs, at this point I get the following exception. I haven't been able...
1
256
by: Mike P | last post by:
Can somebody please tell me if I am going about this task the right way? I have a recordset that I want to read and then perform a test and an update on each record in the recordset. Should I use a datareader to read the data and then a foreach loop to do my test and update? And if so, what would the syntax of my foreach loop be? string strGetCartDetails = "SELECT S.PRODUCTID AS 'Product ID', "; strGetCartDetails += "'£5.00' AS 'Unit...
2
6544
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row *that is going to be deleted* into another table, before the original data is deleted. I am trying to use the RowDeleting method to call an Update or Insert
1
5591
by: Tom | last post by:
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...
20
6086
by: John Bailo | last post by:
I have a c# program that loops through a table on a DB2 database. On each iteration it assigns data to values in the SqlParameter collection. The command text is an INSERT statement to a Sql Server database, run with an .ExecuteQuery I enclosed the loop in a SqlTransaction and commit it at the end. I timed the program and it inserts about 70 records a second...which I think is sort of slow...so I set up some Debug.WriteLines to...
5
2353
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored Procedures with a data reader to add various parameters. However, if I have a stored procedure such as CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3 datatype)
3
2948
by: djc | last post by:
I am playing with the (new to me) sqlDataSource class. I used it to bind data from a database table into a listbox with no code, worked great. Now I need to do an INSERT for every item in a listbox. I used to just code the ado stuff myself in .net 1.1. For example creating connection object, creating sqlCommand object, parameters, etc... I would just create the ado stuff, then loop through the listbox changing the sqlcommand parameter...
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;
0
8459
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
8378
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
8890
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
8577
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
8653
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
7398
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
6206
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
5677
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
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.