johnny 5
johnny 5

Reputation: 20987

Execute Stored Procedure With Nameless Parameters

I was just wondering if there is a way to execute a stored procedure with out naming the parameters. Meaning that C# resolves the parameters in the order they're declared within the stored procedure.

public static DataTable GetRelatedResources(string StoredProcedure, object[] Parameters)
{
   var Results = new DataTable();

   try
   {
        using (SqlConnection conn = new SqlConnection())
        {
            using (SqlCommand cmd = new SqlCommand(ConfigurationManager.ConnectionStrings["MK3Entities"].ConnectionString))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = StoredProcedure;

                if (Parameters!= null)
                {
                   foreach(var Param in Parameters)
                   { 
                       // I Want To Do something like this
                       cmd.Parameters.AddWithValue(Param);
                   }
                }

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(Results);
            }
        }
    }
    catch (Exception ex)
    {
       MMSLogger.Instance.WriteToLog("Exception Executing Stored Procedure:" + ex.Message);
    }

    return Results;
}

Upvotes: 2

Views: 2541

Answers (3)

Remus Rusanu
Remus Rusanu

Reputation: 294227

Execute a command instead, and pass in parameters '@p1', '@p2' etc:

cmd.CommandType = CommandType.Text;
cmd.CommandText = 'exec ' + StoredProcedure;

int i=0;
string comma = ' ';
foreach(var Param in Parameters)
   {
   var paramName = String.Format("@P{0}", i);
   cmd.CommandText += comma + paramName;
   cmd.Parameters.AddWithValue(paramName, Param);
   ++i;
   comma = ', ';
   }

Be aware that AddwithValue is huge performance antipattern. See How Data Access Code Affects Database Performance

Upvotes: 3

BendEg
BendEg

Reputation: 21088

Do you mean some thing like derived parameter. This MSDN article gave an overview:

Parameters can also be derived from a stored procedure using the DbCommandBuilder class. Both the SqlCommandBuilder and OleDbCommandBuilder classes provide a static method, DeriveParameters, which automatically populates the parameters collection of a command object that uses parameter information from a stored procedure. Note that DeriveParameters overwrites any existing parameter information for the command.

A nother solution would be to get the parameter of the stored procedure from the sql-database and than set them in your code. But this need one extra query. To get the parameter use:

select * from dbo.parameters where specific_name='procedure-name'

But in any case, you have to use parameter-names.

Upvotes: 3

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

As per MSDN:

The ParameterName is specified in the form @paramname. You must set ParameterName before executing a SqlCommand that relies on parameters.

Another MSDN article for SqlCommand:

Nameless, also called ordinal, parameters are not supported by the .NET Framework Data Provider for SQL Server.

So answer is no, there is no way to execute a stored procedure without naming the parameters.

Upvotes: 3

Related Questions