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 3795178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:02:39+00:00 2026-05-19T13:02:39+00:00

In VB.NET, I am creating extension methods for the SqlParameterCollection class so that I

  • 0

In VB.NET, I am creating extension methods for the SqlParameterCollection class so that I can add “detailed” parameters in one line of code. I understand that the .AddWithValue method will cause SQL Server to re-evaluate an SQL Server execution plan each time a Stored Procedure is called via an SqlCommand with SqlParameter objects that do not specify the size of alphanumeric data types (i.e. VarChar or NVarChar). In other words, .AddWithValue is a bad idea except for types like INT data types where the size of the data type is constant.

I’ve never used Extension Methods before, so does this code appear to accomplish what I want? The code “works”, but I’m looking for another set of eyes to make sure I’m not ending up with parameters that will force re-evaluation of an SQL Server execution plan.

EDIT: I had to actually add the parameter to the “params” collection, so I’ve updated the code. Now it “works” 😉

Imports System.Runtime.CompilerServices

Public Module AdoParameterExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithNumericValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Value = value

        End With

        params.Add(newParam)

    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddWithStringValue(ByVal params As System.Data.SqlClient.SqlParameterCollection, ByVal parameterName As String, ByVal parameterType As SqlDbType, ByVal size As Integer, ByVal value As Object)

        Dim newParam As New System.Data.SqlClient.SqlParameter

        With newParam

            .ParameterName = parameterName
            .SqlDbType = parameterType
            .Size = size
            .Value = value

        End With

        params.Add(newParam)

    End Sub

End Module

Thanks for your feedback.

  • 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-19T13:02:39+00:00Added an answer on May 19, 2026 at 1:02 pm

    Your extensions look fine. Whether or not the SQL Server execution plan well be re-evaluated won’t be affected by using an Extension method.

    One recommendation I’d make is to return the SqlParameterCollection instead of void (Sub). Parameters can then be added fluently.

    Instead of

    params.AddWithNumericValue(...)
    params.AddWithNumericValue(...)
    params.AddWithNumericValue(...)
    

    you write as

    params.AddWithNumericValue(...).AddWithNumericValue(...).AddWithNumericValue(...)
    

    It’s a preference, but with extensions like this I’ve come to appreciate the style.

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

Sidebar

Related Questions

I am creating a .NET application (C#) that needs to use a lot of
What are some important practices to follow when creating a .NET assembly that is
I asked this Can I automate creating a .NET web application in IIS? a
I am creating an application in .NET that will serve as a second UI
I'd like to override the Serialize methods of the ASP.NET JavaScriptSerializer class. Nothing too
I'm creating a .net custom control and it should be able to load multiple
I am creating ASP.NET textbox controls dynamically. I want to know the difference between
In Visual Studio 2008 (and others) when creating a .NET or silverlight application if
When creating scrollable user controls with .NET and WinForms I have repeatedly encountered situations
I'm creating an asp.net app with just some lite data access from xml files.

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.