Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:18:03+00:00 2026-05-30T17:18:03+00:00

Anyone know of a better way to create and initialize SqlParameter s in VB.NET?

  • 0

Anyone know of a better way to create and initialize SqlParameters in VB.NET? Using 3 lines per variable seems quite excessive. Unfortunately the constructors for this class are rather ridiculous, so I’m thinking of just writing my own sub for initializing each parameter. This is how I’ve been doing it.

Dim ID As New SqlParameter("@ID", SqlDbType.Int)
ID.Value = val
query.Parameters.Add(ID)

Here is the full code that I use for even a simple Store Procedure which only takes a single argument. I have some however which take 15-20 and that’s a LOT of lines of code using this clunky method.

Dim query As New SqlCommand("spGetInitStatusEntry", connection)
query.CommandType = CommandType.StoredProcedure
Dim ID As New SqlParameter("@ID", SqlDbType.Int)
ID.Value = val
query.Parameters.Add(ID)
Dim dt As New DataTable
Dim da As New SqlDataAdapter(query)
da.Fill(dt)

Final Implementation

I created the following Module, as recommended, with two supporting <Extension()>s.

Module TcomExtensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddParameter(ByRef dbCommand As SqlCommand, ByVal parameterName As String, _
            ByVal parameterValue As Object, ByVal dbType As SqlDbType)

        Dim dbParameter As SqlParameter = Nothing
        dbParameter = dbCommand.CreateParameter

        dbParameter.ParameterName = parameterName
        dbParameter.Value = parameterValue
        dbParameter.DbType = dbType

        dbCommand.Parameters.Add(dbParameter)
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToDataTable(ByVal value As SqlCommand) As DataTable
        Dim dt As New DataTable
        Using da As New SqlDataAdapter(value)
            da.Fill(dt)
        End Using
        Return dt
    End Function
End Module

And my implementation becomes as follows:

Dim spPrefill As New SqlCommand("spGetPrefillStatusEntry", connection) With {.CommandType = CommandType.StoredProcedure}
'Only one line per added parameter.
spPrefill.AddParameter("@ID", val, SqlDbType.Int)
'Populate the table.
dt = spPrefill.ToDataTable()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:18:05+00:00Added an answer on May 30, 2026 at 5:18 pm

    With those extension…

     <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddParameterValue(ByRef dbCommand As System.Data.Common.DbCommand, ByVal parameterName As String, ByVal parameterValue As Object)
        dbCommand.AddParameter(parameterName, parameterValue)
    End Sub
    
    
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddParameter(ByRef dbCommand As System.Data.Common.DbCommand, ByVal parameterName As String, Optional ByVal parameterValue As Object = Nothing, Optional ByVal sourceColumn As String = "")
        Dim dbParameter As System.Data.Common.DbParameter = Nothing
    
    
        dbParameter = dbCommand.CreateParameter
    
        dbParameter.ParameterName = parameterName
        dbParameter.Value = parameterValue
        dbParameter.SourceColumn = sourceColumn
    
        dbCommand.Parameters.Add(dbParameter)
    
    End Sub
    

    …you’ll be able to add a parameter with 1 line of code

    Dim query As New SqlCommand("spGetInitStatusEntry", connection)
    query.CommandType = CommandType.StoredProcedure
    query.AddParameterValue("@ID", key)
    Dim dt As New DataTable
    Dim da As New SqlDataAdapter(query)
    da.Fill(dt) 
    

    The function is pretty generic and the parameter type isn’t specified but you can add it as parameter if you really need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way
If anyone know where to download XAMLPad? Or you know better alternative to test
Does anyone know of a better GUI client for displaying Windows System Monitor log
Does anyone know, or better yet have an example, of a WCF service that
Anyone know of a way to capture keyboard events (keyup / keydown) in Portable
Anyone know how to do this without using a third party program? If there
Anyone know this compiler feature? It seems GCC support that. How does it work?
is there anyone know how to monitor a folder using java? or anyone could
Does anyone know of a way to require a serial number or registration of
Anyone know if it's possible to databind the ScaleX and ScaleY of a render

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.