[英]OdbcCommand on Stored Procedure - “Parameter not supplied” error on Output parameter
I'm trying to execute a stored procedure (against SQL Server 2005 through the ODBC driver) and I recieve the following error: 我正在尝试执行存储过程(通过ODBC驱动程序针对SQL Server 2005),并且收到以下错误消息:
Procedure or Function 'GetNodeID' expects parameter '@ID', which was not supplied. 过程或函数“ GetNodeID”需要未提供的参数“ @ID”。
@ID is the OUTPUT parameter for my procedure, there is an input @machine which is specified and is set to null in the stored procedure: @ID是我的过程的OUTPUT参数,在存储过程中指定了一个输入@machine并将其设置为null:
ALTER PROCEDURE [dbo].[GetNodeID]
@machine nvarchar(32) = null,
@ID int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT * FROM Nodes WHERE NodeName=@machine)
BEGIN
SELECT @ID = (SELECT NodeID FROM Nodes WHERE NodeName=@machine)
END
ELSE
BEGIN
INSERT INTO Nodes (NodeName) VALUES (@machine)
SELECT @ID = (SELECT NodeID FROM Nodes WHERE NodeName=@machine)
END
END
The following is the code I'm using to set the parameters and call the procedure: 以下是我用来设置参数和调用过程的代码:
OdbcCommand Cmd = new OdbcCommand("GetNodeID", _Connection);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("@machine", OdbcType.NVarChar);
Cmd.Parameters["@machine"].Value = Environment.MachineName.ToLower();
Cmd.Parameters.Add("@ID", OdbcType.Int);
Cmd.Parameters["@ID"].Direction = ParameterDirection.Output;
Cmd.ExecuteNonQuery();
_NodeID = (int)Cmd.Parameters["@Count"].Value;
I've also tried using Cmd.ExecuteScalar with no success. 我也尝试使用Cmd.ExecuteScalar,但没有成功。 If I break before I execute the command, I can see that @machine has a value. 如果在执行命令之前中断,我可以看到@machine有一个值。
If I execute the procedure directly from Management Studio, it works correctly. 如果我直接从Management Studio执行该过程,它将正常工作。
Any thoughts? 有什么想法吗? Thanks 谢谢
Try replacing : 尝试更换:
OdbcCommand Cmd = new OdbcCommand("GetNodeID", _Connection);
Cmd.CommandType = CommandType.StoredProcedure;
With : 用:
OdbcCommand Cmd = new OdbcCommand("{call GetNodeID(?,?)}", _Connection);
More info : 更多信息 :
http://support.microsoft.com/kb/310130 http://support.microsoft.com/kb/310130
Stored procedure with input parameters and ODBC Connection: 具有输入参数和ODBC连接的存储过程:
create a stored procedure: 创建一个存储过程:
create procedure proc_name @parm1 varchar(20), @parm2 varchar(10) as begin insert into table_name values(@parm1,@parm2);end 创建过程proc_name @ parm1 varchar(20),@ parm2 varchar(10)作为开始插入table_name值(@ parm1,@ parm2);结束
This code works in SQL Server.
此代码在SQL Server.
private void button1_Click(object sender, EventArgs e)
{
string name = txtname.Text;
string num = txtnum.Text;
OdbcConnection con = new OdbcConnection("dsn=naveenk_m5");
OdbcCommand cmd = new OdbcCommand("{call proc1(?,?)}",con);
cmd.Parameters.Add("@parm1", OdbcType.VarChar).Value=name;
cmd.Parameters.Add("@parm2", OdbcType.VarChar).Value = num;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("inserted a row");
}
I'm not exactly sure what you mean by 我不确定你是什么意思
there is an input @machine which is specified and is set to null in the stored procedure 有一个输入@machine已指定,并且在存储过程中将其设置为null
In your proc's signature, this line: 在您的proc签名中,此行:
@machine nvarchar(32) = null
doesn't mean that you're setting @machine
to null inside the proc - it means you're assigning a default value to be used in case the parameter is missing (in this case, null
is the value to be used for a missing param). 并不意味着您在proc内部将@machine
设置为null;这意味着您要分配一个默认值,以防参数丢失(在这种情况下, null
是用于缺少值的值) PARAM)。
Getting the error about @ID
being missing would happen if you were calling this stored procedure without passing any parameters at all ( @machine
would not be flagged as a problem since it has a default value defined). 如果您在不传递任何参数的情况下调用此存储过程,则会发生有关@ID
丢失的错误( @machine
已定义了默认值,因此不会将其标记为问题)。 Your code example looks fine to me - are you sure the stored proc isn't being called from somewhere else in your program (somewhere where no parameters are being added)? 您的代码示例对我来说很好-您确定没有从程序的其他位置(未添加任何参数的地方)调用存储的proc吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.