C# 需要未提供的参数“@ID”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9799524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expects parameter '@ID', which was not supplied?
提问by Azhar
I am sending ID as outparameter but its giving error
我将 ID 作为 outparameter 发送,但它给出了错误
System.Data.SqlClient.SqlException: Procedure or function 'usp_ClientHistoryItem' expects parameter '@ID', which was not supplied.
System.Data.SqlClient.SqlException:过程或函数“usp_ClientHistoryItem”需要未提供的参数“@ID”。
Code
代码
using (SqlCommand cmd = new SqlCommand("dbo.usp_ClientHistoryItem", conn))
{
SqlParameter parameterID = new SqlParameter("@ID", oReservation.Id);
parameterID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parameterID);
cmd.Parameters.Add(new SqlParameter("@PhoneNo", oReservation.ClientPhone));
cmd.Parameters.Add(new SqlParameter("@UserId", oReservation.UserID));
cmd.Parameters.Add(new SqlParameter("@Description", oReservation.Description));
cmd.Parameters.Add(new SqlParameter("@TestId", oReservation.TestId));
cmd.Parameters.Add(new SqlParameter("@StartDate", oReservation.StartDate));
cmd.ExecuteNonQuery();
returnValue = Convert.ToInt32(cmd.Parameters["@ID"].Value);
return returnValue;
}
采纳答案by marc_s
You seem to be calling a stored procedure - yet you've never definedyour SqlCommandto be a stored procedure:
您似乎正在调用存储过程 - 但您从未将您定义SqlCommand为存储过程:
using (SqlCommand cmd = new SqlCommand("dbo.usp_ClientHistoryItem", conn))
{
cmd.CommandType = CommandType.StoredProcedure; // add this line to tell ADO.NET it's a stored procedure!!
If you forget that line, then ADO.NET will try to interpret your stuff as an ad-hoc SQL statement....
如果您忘记了该行,那么 ADO.NET 将尝试将您的内容解释为临时 SQL 语句....
回答by Mujtaba Hassan
Your ID parameter in the stored procedure must be set as OUTPUT parameter. You are just setting it in code not in stored procedure.
您在存储过程中的 ID 参数必须设置为 OUTPUT 参数。您只是在代码中而不是在存储过程中设置它。
回答by Jorge Corradi
Hy guys.
嘿伙计们。
You have to set the property CommandType for the Command to StoredProcedure if that's the case. Otherwise it woun't detect the parameters.
如果是这种情况,您必须将命令的属性 CommandType 设置为 StoredProcedure。否则它不会检测参数。
回答by siva
this one solve my problem may be it may helpful
这个解决了我的问题可能会有所帮助
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandType = CommandType.StoredProcedure;
回答by Altamash Shaikh
One other reason this error is thrown is when the variable names don't match in your stored procedure and code because the code fails to find the parameter to which the value must be passed. Make sure they match:
引发此错误的另一个原因是存储过程和代码中的变量名称不匹配,因为代码无法找到必须向其传递值的参数。确保它们匹配:
Stored procedure:
存储过程:
create procedure getEmployee
@ID
as
Begin
select *
from emp
where id = @ID
End
Code:
代码:
SqlParameter p = new SqlParameter("@ID", id);
cmd.Parameter.Add(p);
The parameter @IDmust match in both code and stored procedure
参数@ID必须在代码和存储过程中匹配
回答by Евгений Елисеев
If you use dapper, you can use this construction
如果您使用dapper,则可以使用此结构
int id = 1;
var parameters = new DynamicParameters();
parameters.Add("@id", id, DbType.Int32, ParameterDirection.Input);
string sqlQuery = "[dbo].[SomeStoredProcedure]";
using (IDbConnection db = new SqlConnection(ConnectionString))
{
var result = await db.QueryAsync<SpResult>(sqlQuery, parameters, commandType: CommandType.StoredProcedure);
}

