473,511 Members | 14,052 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.SqlCommand
Dim m_drSQLTop As SqlClient.SqlDataReader

Do While bContinue
sSQL = "GetPacketDataTop"
m_cmdSQLT = New SqlClient.SqlCommand
With m_cmdSQLT
.Connection = TickServerAdoCon
.CommandType = CommandType.StoredProcedure
.CommandText = sSQL
End With
m_cmdSQLT.Parameters.Add("@sContract", SqlDbType.VarChar, 8)
m_cmdSQLT.Parameters.Add("@dTime1", SqlDbType.DateTime, 8)
m_cmdSQLT.Parameters.Add("@dTime2", SqlDbType.DateTime, 8)
m_cmdSQLT.Parameters("@sContract").Value = txtSymbol.Text
m_cmdSQLT.Parameters("@dTime1").Value = dTime1
m_cmdSQLT.Parameters("@dTime2").Value = dtime2
m_drSQLTop = m_cmdSQLT.ExecuteReader()
If m_drSQLTop.Read Then sOpen = "" & m_drSQLTop("PACKET_DATA")
m_drSQLTop.Close()
m_cmdSQLT.Dispose()
m_cmdSQLT = Nothing
:
dTime1 = DateAdd(DateInterval.Minute, 1, dTime1)
dtime2 = DateAdd(DateInterval.Minute, 1, dTime1)
If DateDiff(DateInterval.Minute, dLast, dTime1) >= 1 Then
bContinue = False
End If
Loop

Mar 8 '07 #1
2 5173
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.SqlCommand
sSQL = "GetPacketDataTop"
With m_cmdSQLT
.Connection = TickServerADOCon
.CommandType = CommandType.StoredProcedure
.CommandText = sSQL
End With

m_cmdSQLT.Parameters.Add("@sContract", SqlDbType.VarChar, 8)
m_cmdSQLT.Parameters.Add("@dTime1", SqlDbType.DateTime, 8)
m_cmdSQLT.Parameters.Add("@dTime2", SqlDbType.DateTime, 8)

Do While bContinue
m_cmdSQLT.Parameters("@sContract").Value = txtSymbol,text
m_cmdSQLT.Parameters("@dTime1").Value = dTime1
m_cmdSQLT.Parameters("@dTime2").Value = dTime2
m_drSQLTop = m_cmdSQLT.ExecuteReader()
If m_drSQLTop.Read Then sOpen = "" &
m_drSQLTop("PACKET_DATA")
m_drSQLTop.Close()

dTime1 = DateAdd(DateInterval.Minute, 1, dTime1)
dTime2 = DateAdd(DateInterval.Minute, 1, dTime1)
If DateDiff(DateInterval.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*********************@h3g2000cwc.googlegrou ps.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.SqlCommand
sSQL = "GetPacketDataTop"
With m_cmdSQLT
.Connection = TickServerADOCon
.CommandType = CommandType.StoredProcedure
.CommandText = sSQL
End With

m_cmdSQLT.Parameters.Add("@sContract", SqlDbType.VarChar, 8)
m_cmdSQLT.Parameters.Add("@dTime1", SqlDbType.DateTime, 8)
m_cmdSQLT.Parameters.Add("@dTime2", SqlDbType.DateTime, 8)

Do While bContinue
m_cmdSQLT.Parameters("@sContract").Value = txtSymbol,text
m_cmdSQLT.Parameters("@dTime1").Value = dTime1
m_cmdSQLT.Parameters("@dTime2").Value = dTime2
m_drSQLTop = m_cmdSQLT.ExecuteReader()
If m_drSQLTop.Read Then sOpen = "" &
m_drSQLTop("PACKET_DATA")
m_drSQLTop.Close()

dTime1 = DateAdd(DateInterval.Minute, 1, dTime1)
dTime2 = DateAdd(DateInterval.Minute, 1, dTime1)
If DateDiff(DateInterval.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
7196
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...
2
8312
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...
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...
2
6531
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...
1
5584
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...
20
6062
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...
5
2344
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...
3
2942
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...
2
2090
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
7144
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...
0
7512
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...
0
5671
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,...
1
5069
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...
0
4741
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...
0
3227
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
1
785
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.