SQL服务器&放大器; .NET支持调用存储过程与参数的值wihout提供参数的名字呢?参数、放大器、存储过程、名字

2023-09-04 01:24:55 作者:你◇№根本不值得√

假设我已经在SQL Server存储过程使用的名称sp_GetData1与PARAMS:

Assume that I have a store procedure in SQL Server with a name sp_GetData1 with params:

@Input1 int
@Input2 string

此存储过程将返回一个DataTable。为了得到数据表中,从这个存储过程,我用ADO.NET。为了执行这个存储过程,我们的必须提供param'name,参数的sqldatatype,和放大器;参数'值

THis stored procedure will return a datatable. To get datatable from this stored procedure, I use ADO.NET. To execute this stored procedure, we must provide param'name, param's sqldatatype, & param' value.

我有一个问题:我们可以在不提供参数的名字和放大器执行(使用ADO.NET)这个存储过程;参数的sqldatatype?那意思,我只需要才能提供的参数值。

感谢。

推荐答案

一种是使用 SqlCommandBuilder.DeriveParameters 以从服务器检索的参数。这将创建SQLParameters给你,然后你只需要填写上该命令的参数集合中的值。

One is to use SqlCommandBuilder.DeriveParameters to retrieve the parameters from the server. This will create the SQLParameters for you and then you just need to populate the values on the parameter collection on the command.

...Build up the cmd with command string and connection
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters[0].Value = input1;
cmd.Parameters[0].Value = input2;

目前是打算到服务器,但至少你不必担心SQL enjection的性能损失。

There is a performance penalty for going to the server but at least you don't have to worry about sql enjection

老提供SQLHelper这样做和缓存的参数,以便只做到了一次。企业库将这样做。完整的ORM也会做类似的事情给你。

The old SQLHelper did this and cached the parameters so it only did it once. Enterprise Library will do the same. Full ORMs also will do similar things for you.