Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQLParameter Array... 1

Status
Not open for further replies.

sqlsamurai

Programmer
May 11, 2005
120
US
Consider this code...

Code:
If a = True Then
   [green]'add sqlparameter to parameter array[/green]
End If

If b = True Then
   [green]'add sqlparameter to parameter array[/green]
End If

If c = True Then
   [green]'add sqlparameter to parameter array[/green]
End If

Each time this code runs the parameter array could change sizes... The first time it could contain only one parameter... the second time it runs it could contain three parameters.

How do i define the parameter array when i dont know how many dimensions I'll need, and how do I add parameters to the array?
 
the parameter array of the commandobject isn't an array but a collection. So it doesn't really care.

Code:
dim comtemp as sqlcommand
If a = True Then
   comtemp.parameters.add(new sqlparameter(...)
End If

If b = True Then
   comtemp.parameters.add(new sqlparameter(...)
End If

If c = True Then
   comtemp.parameters.add(new sqlparameter(...)
End If

but your commandtext will have to have the same number of parameters.

Christiaan Baes
Belgium

"My new site" - Me
 
There is a more .NET specific way, but I can't remember what it is for the moment - but this should do what you need:


Code:
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim arr() As String
    For a As Integer = 0 To 9
      ReDim Preserve arr(a)
      arr(a) = a.ToString
    Next
    For a As Integer = 0 To arr.Length - 1
      MessageBox.Show(arr(a))
    Next

  End Sub


Hope this helps.

[vampire][bat]
 
chrisse1, I'm using the Data Access Application Block and I'm able to just pass in the name of the stored procedure and a parameter array and it figures everything else out... I'm not creating my own command object... the DAAB is doing that for me.
 
then try this. The toarray will give you the array you want in the size it is.

Code:
Dim ap As New List(Of SqlClient.SqlParameter)
If a = True Then
   ap.parameters.add(new sqlparameter(...)
End If

If b = True Then
   ap.parameters.add(new sqlparameter(...)
End If

If c = True Then
   ap.parameters.add(new sqlparameter(...)
End If

ap.ToArray

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top