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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:38:44+00:00 2026-05-12T10:38:44+00:00

I have the following method that is supposed to be a generic Save to

  • 0

I have the following method that is supposed to be a generic “Save to SQL” method for my application.

protected void EjecutarGuardar(string ProcedimientoAlmacenado, object[] Parametros)
        {
            SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

            SqlCommand Command = Connection.CreateCommand();             
            Command.CommandType = CommandType.StoredProcedure;
            foreach (object X in Parametros)
            {
                Command.Parameters.Add(X);
            }            

            Connection.Open();
            Command.ExecuteNonQuery();
            Connection.Close();

            Connection.Dispose();
        }

I have to pass the NAME of the StoredProcedure and an Array filled with the parameters. I’m kind of lost at this point. Where should I use the NAME of the stored procedure “ProcedimientoAlmacenado”?

I’m thinking maybe Command.Command?????something somethign? But I’m lost there. Any help?

Edit: For simplicities sake let’s say a I have a stored procedure called “ABC” in my database. How could I associate it to my SqlCommand “Command” in my code?

  • 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-12T10:38:44+00:00Added an answer on May 12, 2026 at 10:38 am

    Command.CommandText= ProcedimientoAlmacenado

    The parameters must have names too. Does the Parametros array contains SqlParameter objects or generic C# objects?

    If the parameters are generic C# objects, is better to pass in a dictionary of names and values:

    protected void EjecutarGuardar(string ProcedimientoAlmacenado, 
        Dictionary<string, object> Parametros)
    {
        using (SqlConnection Connection = new SqlConnection(...))
        {
            Connection.Open();
            SqlCommand Command = Connection.CreateCommand()
            Command.CommandText = ProcedimientoAlmacenado;
            Command.Connection = Connection;       
            Command.CommandType = CommandType.StoredProcedure;
            foreach (string name in Parametros.Keys)
            {
              Command.Parameters.AddWithValue(name, Parametros[name] ?? DBNull.Value);
            }            
            Command.ExecuteNonQuery();
        }
    }
    

    This is a quick and dirty approach. Note that this approach usually has problems because AddWithValue will pass in a parameter of type NVARCHAR for a string, not VARCHAR, and with ad-hoc SQL this can cause index SARG-ability problems on VARCHAR columns (because the conversion will be always from VARCHAR to NVARCHAR and not vice-versa). However with stored procedures is not such a problem because procedures have types parameters and thus a force coercion happens to VARCHAR if the procedure was created with parameter type VARCHAR.

    You will also have problems around passing NULL parameters, so you’ll need to do something like, the parameter has to be DBNull.Value not null:

    Command.Parameters.AddWithValue(name, Parametros[name] ?? DBNull.Value);
    

    On high performance systems this approach also pollutes the execution cache unnecessarily because the AddWithValue will pass parameters of type NVARCHAR(<exact length of the string>), not NVARCHAR(<length of the database type>). So Paramaters.AddWithValue("@name", "John") and Parameters.AddwithValue("@name", "Doe") will create two distinct plans in the cache because one is invoked with a parameter of type NVARCHAR(4), the other with a parameter NVARCHAR(3) and they are seen by the SQL plan cache as different types. This is not a problem on simple projects, but on more complex and high performance ones it is recommended to set the parameter types explicitly.

    My recommendation would be to avoid this kind of generic one-size-fits-all procedures and instead write a data access layer with explicit C# wrapper for each database procedure, with properly types parameters. A strongly typed dataset can actually do this, the other alternative (my favorite and what I always use) is to generate the entire data acces slayer from an XML file using an XSLT stylesheet that creates the C# wrappers. The source XML is, of course, extracted from the database meta data itself.

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

Sidebar

Related Questions

Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I have the following problem in ASP.NET: there is a form that contains a
If it possible to write byte code for a method that is supposed to
I have the following code, which does not work correctly for some reason that
I have the following method_missing code implemented in a model: # class Thought def
I have the following period 1month 5d 22h 35m 39s, which I want to
There is a design problem like this. Suppose you have a set of class
In the app im creating there are many pages that look mostly the same
I am creating a program where a change in a value causes the color
I'm having a hard time getting what I want out of the python subprocess

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.