Solved

Vb.net Sql Variable number of Parameters

Posted on 2014-03-19
3
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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