有没有办法通过的SqlCommand(ADO.NET)传递未命名参数存储过程? [英] Is there any way to pass unnamed parameter to stored procedure through SqlCommand (ADO.NET)?
问题描述
我写了一个简单的包装,使用的SqlCommand类型== StoredProcedure的SP呼叫。我只是想通过参数有没有指定其名字。我怎样才能做到这一点? cmd.Parameters.Add(参数)不起作用,它只有SqlParameter的实例。 感谢您的回复,
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.Add(param) doesn't work, it gets only SqlParameter instance. Thank you for replies,
推荐答案
如何试图用<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.deriveparameters.aspx"相对=nofollow> SqlCommandBuilder.DeriveParameters 来填充参数集合?
How about trying to use SqlCommandBuilder.DeriveParameters to populate the parameters collection?
用法:
// 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.
这篇关于有没有办法通过的SqlCommand(ADO.NET)传递未命名参数存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!