?
Solved

Vb.net Sql Variable number of Parameters

Posted on 2014-03-19
3
Medium Priority
?
564 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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