Solved

Vb.net Sql Variable number of Parameters

Posted on 2014-03-19
3
553 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp web application 3 43
linq .concat equivalent with expression syntax 4 26
VB.NET System.NullReferenceException 1 26
c#, datatable, aspx 4 25
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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