473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SQLParameters and loops???

Is there an easy why to build this in a loop rather than having to declare
every single SQLparameter, value etc etc

I would be really grateful for any advice!

Thanks
... CODE ...

Sub UploadData(ByVal sender As Object, ByVal e As EventArgs)

Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("AddMenu", MyConn)
cmd.CommandType = CommandType.StoredProcedure
Dim Dateparam As New SqlParameter("@Date", SqlDbType.NVarChar, 255)
' Dim i As Integer
' For i = 0 To 11
Dim Item1param As New SqlParameter("@mItem1", SqlDbType.NVarChar, 255)
Dim Item2param As New SqlParameter("@mItem2", SqlDbType.NVarChar, 255)
Dim Item3param As New SqlParameter("@mItem3", SqlDbType.NVarChar, 255)
Dim Item4param As New SqlParameter("@mItem4", SqlDbType.NVarChar, 255)
Dim Item5param As New SqlParameter("@mItem5", SqlDbType.NVarChar, 255)
Dim Item6param As New SqlParameter("@mItem6", SqlDbType.NVarChar, 255)
Dim Item7param As New SqlParameter("@mItem7", SqlDbType.NVarChar, 255)
Dim Item8param As New SqlParameter("@mItem8", SqlDbType.NVarChar, 255)
Dim Item9param As New SqlParameter("@mItem9", SqlDbType.NVarChar, 255)
Dim Item10param As New SqlParameter("@mItem10", SqlDbType.NVarChar,
255)

Dim Price1param As New SqlParameter("@mPrice1", SqlDbType.NVarChar,
255)
Dim Price2param As New SqlParameter("@mPrice2", SqlDbType.NVarChar,
255)
Dim Price3param As New SqlParameter("@mPrice3", SqlDbType.NVarChar,
255)
Dim Price4param As New SqlParameter("@mPrice4", SqlDbType.NVarChar,
255)
Dim Price5param As New SqlParameter("@mPrice5", SqlDbType.NVarChar,
255)
Dim Price6param As New SqlParameter("@mPrice6", SqlDbType.NVarChar,
255)
Dim Price7param As New SqlParameter("@mPrice7", SqlDbType.NVarChar,
255)
Dim Price8param As New SqlParameter("@mPrice8", SqlDbType.NVarChar,
255)
Dim Price9param As New SqlParameter("@mPrice9", SqlDbType.NVarChar,
255)
Dim Price10param As New SqlParameter("@mPrice10",
SqlDbType.NVarChar, 255)

Dateparam.Value = tbDate.Text
Item1param.Value = tbMenuItem1.Text
Item2param.Value = tbMenuItem2.Text
Item3param.Value = tbMenuItem3.Text
Item4param.Value = tbMenuItem4.Text
Item5param.Value = tbMenuItem5.Text
Item6param.Value = tbMenuItem6.Text
Item7param.Value = tbMenuItem7.Text
Item8param.Value = tbMenuItem8.Text
Item9param.Value = tbMenuItem9.Text
Item10param.Value = tbMenuItem10.Text

Price1param.Value = tbMenuPrice1.Text
Price2param.Value = tbMenuPrice2.Text
Price3param.Value = tbMenuPrice3.Text
Price4param.Value = tbMenuPrice4.Text
Price5param.Value = tbMenuPrice5.Text
Price6param.Value = tbMenuPrice6.Text
Price7param.Value = tbMenuPrice7.Text
Price8param.Value = tbMenuPrice8.Text
Price9param.Value = tbMenuPrice9.Text
Price10param.Value = tbMenuPrice10.Text
cmd.Parameters.Add(Dateparam)
cmd.Parameters.Add(Item1param)
cmd.Parameters.Add(Item2param)
cmd.Parameters.Add(Item3param)
cmd.Parameters.Add(Item4param)
cmd.Parameters.Add(Item5param)
cmd.Parameters.Add(Item6param)
cmd.Parameters.Add(Item7param)
cmd.Parameters.Add(Item8param)
cmd.Parameters.Add(Item9param)
cmd.Parameters.Add(Item10param)

cmd.Parameters.Add(Price1param)
cmd.Parameters.Add(Price2param)
cmd.Parameters.Add(Price3param)
cmd.Parameters.Add(Price4param)
cmd.Parameters.Add(Price5param)
cmd.Parameters.Add(Price6param)
cmd.Parameters.Add(Price7param)
cmd.Parameters.Add(Price8param)
cmd.Parameters.Add(Price9param)
cmd.Parameters.Add(Price10param)
' Next

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
Nov 19 '05 #1
1 1642
Hi Tim,

There are 3 aspects to using variables that are classes:

1. Declare the variable (Dim DateParam As SqlParameter)
2. Instantiate the variable (DateParam = New SqlParameter())
3. Assign the variable's value (DateParam.Name =...DateParam.Type =...)

In your code, you are declaring, instantiating, and assigning your variables
all in one swell foop. That is perfectly acceptable, however, it does result
in a small performance degradation.

It is better to declare variables outside of a loop if you intend to use
them inside the loop. Declare and instantiate your variables at the top of
your function. Assign them in the loop. Or declare them at the top of your
function, and instantiate and assign them in the loop. The first option will
yield the best performance. The second will make smaller code that takes
less time to write. Perhaps the best compormise would be to declare and
instantiate them at the top of the code, using the constructor overload that
takes a name, type, and size. Then assign the value inside the loop.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:3E**********************************@microsof t.com...
Is there an easy why to build this in a loop rather than having to declare
every single SQLparameter, value etc etc

I would be really grateful for any advice!

Thanks
.. CODE ...

Sub UploadData(ByVal sender As Object, ByVal e As EventArgs)

Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("AddMenu", MyConn)
cmd.CommandType = CommandType.StoredProcedure
Dim Dateparam As New SqlParameter("@Date", SqlDbType.NVarChar, 255)
' Dim i As Integer
' For i = 0 To 11
Dim Item1param As New SqlParameter("@mItem1", SqlDbType.NVarChar,
255)
Dim Item2param As New SqlParameter("@mItem2", SqlDbType.NVarChar,
255)
Dim Item3param As New SqlParameter("@mItem3", SqlDbType.NVarChar,
255)
Dim Item4param As New SqlParameter("@mItem4", SqlDbType.NVarChar,
255)
Dim Item5param As New SqlParameter("@mItem5", SqlDbType.NVarChar,
255)
Dim Item6param As New SqlParameter("@mItem6", SqlDbType.NVarChar,
255)
Dim Item7param As New SqlParameter("@mItem7", SqlDbType.NVarChar,
255)
Dim Item8param As New SqlParameter("@mItem8", SqlDbType.NVarChar,
255)
Dim Item9param As New SqlParameter("@mItem9", SqlDbType.NVarChar,
255)
Dim Item10param As New SqlParameter("@mItem10", SqlDbType.NVarChar,
255)

Dim Price1param As New SqlParameter("@mPrice1", SqlDbType.NVarChar,
255)
Dim Price2param As New SqlParameter("@mPrice2", SqlDbType.NVarChar,
255)
Dim Price3param As New SqlParameter("@mPrice3", SqlDbType.NVarChar,
255)
Dim Price4param As New SqlParameter("@mPrice4", SqlDbType.NVarChar,
255)
Dim Price5param As New SqlParameter("@mPrice5", SqlDbType.NVarChar,
255)
Dim Price6param As New SqlParameter("@mPrice6", SqlDbType.NVarChar,
255)
Dim Price7param As New SqlParameter("@mPrice7", SqlDbType.NVarChar,
255)
Dim Price8param As New SqlParameter("@mPrice8", SqlDbType.NVarChar,
255)
Dim Price9param As New SqlParameter("@mPrice9", SqlDbType.NVarChar,
255)
Dim Price10param As New SqlParameter("@mPrice10",
SqlDbType.NVarChar, 255)

Dateparam.Value = tbDate.Text
Item1param.Value = tbMenuItem1.Text
Item2param.Value = tbMenuItem2.Text
Item3param.Value = tbMenuItem3.Text
Item4param.Value = tbMenuItem4.Text
Item5param.Value = tbMenuItem5.Text
Item6param.Value = tbMenuItem6.Text
Item7param.Value = tbMenuItem7.Text
Item8param.Value = tbMenuItem8.Text
Item9param.Value = tbMenuItem9.Text
Item10param.Value = tbMenuItem10.Text

Price1param.Value = tbMenuPrice1.Text
Price2param.Value = tbMenuPrice2.Text
Price3param.Value = tbMenuPrice3.Text
Price4param.Value = tbMenuPrice4.Text
Price5param.Value = tbMenuPrice5.Text
Price6param.Value = tbMenuPrice6.Text
Price7param.Value = tbMenuPrice7.Text
Price8param.Value = tbMenuPrice8.Text
Price9param.Value = tbMenuPrice9.Text
Price10param.Value = tbMenuPrice10.Text
cmd.Parameters.Add(Dateparam)
cmd.Parameters.Add(Item1param)
cmd.Parameters.Add(Item2param)
cmd.Parameters.Add(Item3param)
cmd.Parameters.Add(Item4param)
cmd.Parameters.Add(Item5param)
cmd.Parameters.Add(Item6param)
cmd.Parameters.Add(Item7param)
cmd.Parameters.Add(Item8param)
cmd.Parameters.Add(Item9param)
cmd.Parameters.Add(Item10param)

cmd.Parameters.Add(Price1param)
cmd.Parameters.Add(Price2param)
cmd.Parameters.Add(Price3param)
cmd.Parameters.Add(Price4param)
cmd.Parameters.Add(Price5param)
cmd.Parameters.Add(Price6param)
cmd.Parameters.Add(Price7param)
cmd.Parameters.Add(Price8param)
cmd.Parameters.Add(Price9param)
cmd.Parameters.Add(Price10param)
' Next

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub

Nov 19 '05 #2

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

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
4
by: Gav | last post by:
Hi All, Can somebody tell me the advantage of using SqlParameters over simple putting the paratmeters in the sql string: ie Getsomething(int nSomeNumber) { string sSqlStatement= "Select *...
1
by: Tim::.. | last post by:
Can someone please tell me how I build an array with all my SQLParameters! I want to do something like the example shown below... (" I know it doesn't work!") I would like to generate all the...
2
by: tshad | last post by:
I have an SqlParameter array that I want to reuse after I have used it. For example, I have the following code that calls my generic db routines: ***********************************************...
5
by: Patrick.O.Ige | last post by:
I have a parameter below and i'm passing the value via Store procedure Cmd.Parameters.Add(New SqlParameter("@ProductID", SqlDbType.Int, 1)).Value = 104 But as you can see the value "104" is hard...
6
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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,...
0
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...

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.