Is there any way to pass unnamed parameter to stored procedure through SqlCommand (ADO.NET)?
I write a simple wrapper for sp calls using SqlCommand Type==StoredProcedure. I just want to pass parameter there without specifying its name. How can i do it? cmd.Parameters.A开发者_运维百科dd(param) doesn't work, it gets only SqlParameter instance. Thank you for replies,
How about trying to use SqlCommandBuilder.DeriveParameters to populate the parameters collection?
Usage :
// 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 would obviously need to then populate the values for each parameter.
AFAIK, you can't pass a Parameter without ParameterName. However, if you use Enterprise Library Data Access Application Block, you can do something like this:
Database db = DatabaseFactory.CreateDatabase("DatabaseName");
DbCommand cmd = new SqlCommand("SP Name Here");
cmd.CommandType = CommandType.StoredProcedure;
db.DiscoverParameters(cmd);
Now your Command object will contain all the parameter information.
精彩评论