简体   繁体   English

仅使用名称和类型创建 SqlParameter(未分配值)

[英]Create SqlParameter with name and type only (no value assigned)

I am having issue to use SqlClient to call some sql query with parameter declared but not assigned.我在使用 SqlClient 调用一些 sql 查询时遇到问题,其中参数已声明但未分配。 The query I want to run is similar to below我要运行的查询类似于下面

DECLARE @ID int;
SELECT @ID = OrderId
FROM Orders
ORDER BY OrderId DESC;
SELECT @ID;

However, when I created SqlParameter as following但是,当我创建 SqlParameter 如下

SqlParameter parameter = new SqlParameter("ID", SqlDbType.Int);
sqlcommand.Parameters.Add(parameter);

I got error showing "The parameterized query '(@ID int)SELECT @ID = OrderId FROM Orders ORDER BY OrderId DES' expects the parameter '@ID', which was not supplied."我收到错误消息,显示“参数化查询 '(@ID int)SELECT @ID = OrderId FROM Orders ORDER BY OrderId DES' 需要参数 '@ID',但未提供该参数。”

Any suggestions on how to create SqlParameter without any value assigned in such case?在这种情况下如何创建没有分配任何值的 SqlParameter 有什么建议吗?

In your case you have to specify that @ID is an output parameter.在您的情况下,您必须指定 @ID 是 output 参数。

SqlParameter parameter = new SqlParameter("ID", SqlDbType.Int)
{
    Direction = ParameterDirection.Output
}

When the query will be executed you can retrieve that value via当查询将被执行时,您可以通过检索该值

parameter.Value

It will return an instance of the object type.它将返回 object 类型的实例。

First things first, it doesn't appear your query will return a single int, but many result rows that each contain an OrderId .首先,您的查询似乎不会返回单个 int,而是返回许多结果行,每个结果行都包含一个OrderId You are selecting OrderId from every row in the table and trying to assign it to a single int variable - this won't work.您正在从表中的每一行中选择 OrderId 并尝试将其分配给单个 int 变量 - 这不起作用。 If you just want the greatest OrderId, you can try SELECT TOP 1 @ID = OrderId as seen below.如果您只想要最大的 OrderId,您可以尝试SELECT TOP 1 @ID = OrderId ,如下所示。

To answer the question, the other answers are correct that you need to specify the parameter direction as output.要回答这个问题,其他答案是正确的,您需要将参数方向指定为output。 Output parameters are different than input parameters. Output 参数与输入参数不同。

SELECT TOP (1)
    @id = OrderId
FROM 
    Orders
ORDER BY 
    OrderId DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlParameterCollection仅接受非空的SqlParameter类型对象。 参数名称:值 - The SqlParameterCollection only accepts non-null SqlParameter type objects. Parameter name: value 如何修改对象 SqlParameter 类型的 DbType 值 - How to modify the DbType value for object SqlParameter type SqlParameterCollection 只接受非空的 SqlParameter 类型对象,而不接受 Microsoft.Data.SqlClient 中的 SqlParameter 对象 - The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects in Microsoft.Data.SqlClient System.InvalidCastException:“SqlParameterCollection 只接受非空 SqlParameter 类型对象,不接受 SqlParameter 对象。” - System.InvalidCastException: 'The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.' 是否可以从SqlDbType.Xml类型的输出SqlParameter创建XmlReader? - Is it possible to create an XmlReader from output SqlParameter of type SqlDbType.Xml? 为什么 SqlParameter 名称/值构造函数将 0 视为空? - Why does the SqlParameter name/value constructor treat 0 as null? SqlParameter和数据类型问题 - Issue with SqlParameter and data type SqlParameter 值还是 SqlValue? - SqlParameter Value or SqlValue? SqlParameter设置值不起作用 - SqlParameter set value not working 从SqlParameter转换为SQL类型 - Going from an SqlParameter to the SQL type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM