Solved

Vb.net Sql Variable number of Parameters

Posted on 2014-03-19
3
550 Views
Last Modified: 2014-03-22
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

Open in new window

0
Comment
Question by:rflorencio
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39939492
You can add as many parameters as you like, as long as there are place holders for them in your query.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 39939494
this is not directly supported.

workarounds include:
-use a table value parameter (http://www.emoreau.com/Entries/Articles/2010/11/SQL-Table-Value-parameters.aspx)
-use a delimited string and parse it
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 39940047
One option is to pass this function a list of sql parameters so it just uses those parameters. Then the calling code can add any number of parameters and just pass the list to this function.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question