Is there a way to pass an unnamed parameter to a stored procedure through SqlCommand (ADO.NET)?

I am writing a simple wrapper for sp calls using SqlCommand Type == StoredProcedure. I just want to pass the parameter there without specifying its name. How should I do it? cmd.Parameters.Add (param) does not work, it receives only an instance of SqlParameter. Thanks for answers,

+3
source share
2 answers

How to try using SqlCommandBuilder.DeriveParameters to populate a collection of parameters?

Using:

// Create Command
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = storedProcedureName;

// Open Connection
connection.Open();

// Discover Parameters for Stored Procedure and populate command.Parameters Collection.
// Causes Rountrip to Database.
SqlCommandBuilder.DeriveParameters(command);

You will obviously need to fill in the values ​​for each parameter.

+3
source

AFAIK, ParameterName. , Enterprise Library, - :

        Database db = DatabaseFactory.CreateDatabase("DatabaseName");
        DbCommand cmd = new SqlCommand("SP Name Here");
        cmd.CommandType = CommandType.StoredProcedure;
        db.DiscoverParameters(cmd);

Command .

0

Source: https://habr.com/ru/post/1786607/


All Articles