使用企业库并以不同方式添加输出参数

发布于 2024-12-31 21:22:46 字数 492 浏览 5 评论 0原文

我正在使用 Enterprise Library 来查询数据库,并使用 GetStoredProcCommand 来查询数据库。当我想使用输出参数时,问题就出现了。

 DbCommand cmd = db.GetStoredProcCommand("storedProcedureName", param1, param2, param3, ...);

现在,如果我想添加输出参数,我不能简单地编写 db.AddOutParameter(cmd, "@TotalNumber", DbType.Int64, sizeof(Int64));

我必须添加所有输入参数使用 db.AddInParameter(cmd, "param1", DbType.Int32, param1);

如果您有 10 个输入参数,则在代码中将它们一一添加即可不像好吧,你只是希望你能回去一次将它们全部添加到同一个函数调用中。有没有办法在使用输出参数的同时做到这一点?

I am using Enterprise Library to query database and usin GetStoredProcCommand to query database. The problem arises when I want to use output parameter.

 DbCommand cmd = db.GetStoredProcCommand("storedProcedureName", param1, param2, param3, ...);

Now, if I want to add output parameter, I can't simply write db.AddOutParameter(cmd, "@TotalNumber", DbType.Int64, sizeof(Int64));

I would have to add all the input parameters using db.AddInParameter(cmd, "param1", DbType.Int32, param1);

If you have 10 input parameters, adding them one by one in your code does not like alright and you just wish you could go back to adding them all in the same function call at once. Is there a way of doing this while also use output parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2025-01-07 21:22:46

基本上,您需要做的就是为输出参数传递一个 null (或任何真正的东西),并且您不必为 db.AddOutParameter 烦恼

。代码>DbCommand GetStoredProcCommand(字符串storedProcedureName,
params 对象[] 参数值)

将发现存储过程的参数,并按位置顺序分配值。

他们没有告诉您的是,数据库参数的计数与您将获得的参数不同。Resources.ExceptionMessageParameterMatchFailure 因此,对于每个输出数据库参数,您仍然需要传递一些东西。

他们也没有告诉您的是,当它分配值时,它会检查数据库参数的方向。如果它的输出只是跳过你的值。

Basically all you need to do is pass a null (or anything really) for the output parameter and you don't have to bother with db.AddOutParameter

From the comments on DbCommand GetStoredProcCommand(string storedProcedureName,
params object[] parameterValues)

The parameters for the stored procedure will be discovered and the values are assigned in positional order.

What they don't tell you is that count of the Db Parameters isn't the same as the params you'll get a Resources.ExceptionMessageParameterMatchFailure So for every output DB Parameter you still need to pass somthing.

What they also don't tell you is that when its assigning the value it checks the direction of the database parameter. If its output it just skips over your value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文