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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:38:42+00:00 2026-05-30T03:38:42+00:00

So in C# to use a stored procedure I have code like the following

  • 0

So in C# to use a stored procedure I have code like the following (connection code omitted):

 string sql = "GetClientDefaults";

 SqlCommand cmd = new SqlCommand(sql);
 cmd.CommandType = CommandType.StoredProcedure;    //<-- DO I NEED THIS??
 cmd.Parameters.AddWithValue("@computerName", computerName);

Where sql is the name of a stored procedure. Now, this code seems to work just fine with and without the commented line.

So, do I need this line? Is there some performance (or other) benefit to setting this? Is there a benefit to NOT setting it or setting it to Text?

  • 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-30T03:38:44+00:00Added an answer on May 30, 2026 at 3:38 am

    According to the tests in this blog post SQL Server will do the parameterization for you, by wrapping your statement in sp_executesql, when you use CommandType.Text. But when you use CommandType.StoredProcedure you will parameterize it and thereby saving the database some work. The latter method is faster.

    Edit:

    Setup

    I’ve done some tests myself and here are the results.

    Create this procedure:

    create procedure dbo.Test
    (
       @Text1 varchar(10) = 'Default1'
      ,@Text2 varchar(10) = 'Default2'
    )
    as
    begin
       select @Text1 as Text1, @Text2 as Text2
    end
    

    Add a trace to it using SQL Server Profiler.

    And then call it using the following code:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main()
            {
                CallProcedure( CommandType.Text );
                CallProcedure( CommandType.StoredProcedure );
            }
    
            private static void CallProcedure(CommandType commandType)
            {
                using ( SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;") )
                {
                    connection.Open();
                    using ( SqlCommand textCommand = new SqlCommand("dbo.Test", connection) )
                    {
                        textCommand.CommandType = commandType;
                        textCommand.Parameters.AddWithValue("@Text1", "Text1");
                        textCommand.Parameters.AddWithValue("@Text2", "Text2");
                        using ( IDataReader reader = textCommand.ExecuteReader() )
                        {
                            while ( reader.Read() )
                            {
                                Console.WriteLine(reader["Text1"] + " " + reader["Text2"]);
                            }
                        }
                    }
                }
            }
        }
    }
    

    Results

    In both cases the calls are made using RPC.

    Here’s what the trace reveals using CommandType.Text:

    exec sp_executesql N'dbo.Test',N'@Text1 nvarchar(5),@Text2 nvarchar(5)',@Text1=N'Text1',@Text2=N'Text2'
    

    And here is the result using CommandType.StoredProcedure:

    exec dbo.Test @Text1=N'Text1',@Text2=N'Text2'
    

    As you can see the text-call is wrapped in a call to sp_executesql so that it is properly parameterized. This will of course create a slight overhead, and thus my previous statement that using CommandType.StoredProcedure is faster still stands.

    Another noteworthy thing, and which is also kind of a deal breaker here, is that when I created the procedure without default values I got the following error:

    Msg 201, Level 16, State 4, Procedure Test, Line 0 Procedure or
    function ‘Test’ expects parameter ‘@Text1’, which was not supplied.

    The reason for this is how the call to sp_executesql is created, as you can see the parameters are declared and initialized, but they are not used. For the call to work, it should have looked like this:

    exec sp_executesql N'dbo.Test @Text1, @Text2',N'@Text1 nvarchar(5),@Text2 nvarchar(5)',@Text1=N'Text1',@Text2=N'Text2'
    

    Meaning, when you’re using CommandType.Text you have to add the parameters to the CommandText unless you always want the default values to be used.

    So, to answer your question

    1. Using CommandType.StoredProcedure is faster.
    2. If you’re using CommandType.Text, then you’ll have to add the parameter names to the call to the procedure unless you want the default values to be used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following c# code to call stored procedure testproc , but when
I have two stored procedures I wish to use in my stored procedure, but
I have an ETL process that involves a stored procedure that makes heavy use
Is it a best practice to use stored procedure for every single SQL call
I can use the PRINT statement in a stored procedure to debug my code.
I am trying to use the following stored procedure to obtain databases from a
I'm using dbms_scheduler to execute a PL/SQL stored procedure. I would like to be
I've got code similar to the following in a stored procedure that inserts a
We have a helper class that we use to call stored procs on SQL
Below is my stored procedure. I want use stored procedure select all row of

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.