rp
asked on
Vb.net Sql Variable number of Parameters
I have a function that can take two or more parameters related to database fields. It is possible for an automatic way to pass the number of desired parameters and treat them with sql parameters
Public Shared Function GetData(ByVal User As String, ByVal Qnt As String, ..... variable number of parameters) As Boolean
Dim conn As SqlConnection = DB.DbInit()
Dim cmd As SqlCommand
Dim reader As SqlDataReader
cmd = New SqlCommand("select * from content where user=@user and Quant=@qnt", conn)
cmd.Parameters.Add("@user", SqlDbType.NVarChar, 50)
cmd.Parameters("@user").Value = User
cmd.Parameters.Add("@quant", SqlDbType.Int)
cmd.Parameters("@quant").Value = Qnt
' SOMETIMES I NEED MORE THEN 2 PARAMETERS (variable number of parameters)
cmd.Parameters.Add("@Credit", SqlDbType.Decimal)
cmd.Parameters("@Credit").Value = Credit
cmd.Parameters.Add("@ExpDate", SqlDbType.Date)
cmd.Parameters("@ExpDate").Value = ExpDate
Try
conn.Open()
reader = cmd.ExecuteReader
If reader.Read() Then
.....................
End If
reader.Close()
Catch err As Exception
Finally
conn.Close()
End Try
End Function
You can add as many parameters as you like, as long as there are place holders for them in your query.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.