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

  • Home
  • SEARCH
  • 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 3677388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:12:56+00:00 2026-05-19T03:12:56+00:00

There is a related question to this: What's the best method to pass parameters

  • 0

There is a related question to this:

What's the best method to pass parameters to SQLCommand?

But I am wanting to know what the differences are and if there are any problems with the different ways.

I usually use a structure something like this:

using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(SQL, conn))
{
     cmd.CommandType = CommandType.Text;
     cmd.CommandTimeout = Settings.Default.reportTimeout;
     cmd.Parameters.Add("type", SqlDbType.VarChar, 4).Value = type;

     cmd.Connection.Open();

     using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
     {
         adapter.Fill(ds);
     }
      //use data                    
}

Now there are several ways to add the cmd parameters and I am wondering which is best:

cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob";
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob";
cmd.Parameters.Add("@Name").Value = "Bob";
cmd.Parameters.AddWithValue("@Name", "Bob");

Having the length of the field in the passing of the varchars I assume is not preferable as it’s a magic value which may be changed later at the database. Is this correct? Does it cause any issue passing a varchar in this way (performance or other), I assume it defaults to varchar(max) or the database equivalent. I am reasonably happy this will work.

The part that concerns me more is the loss of the SqlDbType enum if I am using the third or fourth options I listed above I am not supplying a type at all. Are there cases where this won’t work I can imagine problems with varchar being incorrectly cast to char or vice versa or perhaps issues with decimal to money….

In terms of the database the field type I would say is much less likely to change than the length so is it worth retaining?

  • 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-19T03:12:57+00:00Added an answer on May 19, 2026 at 3:12 am

    In my experience, I would make sure I do these things:

    • make sure it’s you that defines the data type for the parameter. ADO.NET does a decent job at guessing, but in some cases, it can be terribly off – so I would avoid this method:

      cmd.Parameters.Add("@Name").Value = "Bob";
      cmd.Parameters.AddWithValue("@Name", "Bob");
      

      Letting ADO.NET guess the type of the parameter by the value passed is tricky, and if it’s off for any reason, those are really tricky bugs to track and find! Imagine what happens when you pass in a DBNull.Value – what datatype should ADO.NET pick for that?

      Just be explicit – say what type it is you want!

    • if you’re using string parameters, make sure to explicitly define the length – so I would avoid this method, too:

      cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob";
      

      If you don’t provide a length, ADO.NET might default to some arbitrary value, or the length of the string passed in as a value, or something else – you’re never quite sure. And if your length doesn’t match what the stored proc really expects, you might see conversion and other unpleasant surprises. So if you define a string, define its length, too!

    So in your case, the only approach that really works for me is this one here:

    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob";
    

    because it a) defines the data type to use explicitly, and b) defines the length of the string explicitly.

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

Sidebar

Related Questions

There is another possibly related question on this but it didn't have answers and
This is related to another open question of mine . While there aren't any
I've been using the search function but I think there isn't any related question
This question is related with one of my earlier questions.. Previous Post In there
This question is related to a previous post . Is there something comparable to
I want to expand on the functionality related to this question . Is there
I've look for this, and there's some related questions, but no one gives me
There's lots of questions on SO related to this, but the ones I scanned
This question is an extension of https://stackoverflow.com/q/438163/1168342 I'd like to know specifically if there's
This is kind of related to my previous question, but not really. I have

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.