简体   繁体   English

存储过程上的OdbcCommand-输出参数上出现“未提供参数”错误

[英]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.

相关问题 存储过程期望未提供的参数 - Stored procedure expects parameter that was not supplied C# 使用 output 变量调用存储过程时出错,未提供参数 - C# getting error calling stored procedure with an output variable, parameter not supplied 存储过程需要参数但实际提供了参数? - Stored procedure expects parameter but parameter is actually supplied? 未提供错误参数的ASP.NET MVC调用存储过程 - ASP.NET MVC calling stored procedure with error parameter not supplied 检索存储过程的输出参数值时出错 - Error in retrieving the value of the output parameter of the stored procedure 存储过程或函数需要未提供的参数 - Stored procedure or function expects parameter which was not supplied 初始化存储过程输出参数 - Initializing stored procedure output parameter 从C#代码调用存储过程时,出现SQL错误“过程或函数需要参数,但未提供” - Sql error “Procedure or function expects parameter , which was not supplied” when calling a stored procedure from c# code Mocking 带有 output 参数的存储过程 - Mocking a stored procedure with an output parameter 存储的过程显示错误“过程或函数'Dlete_Selected'期望参数'@id',未提供” - Stored procedure showing an error“ Procedure or function 'Dlete_Selected' expects parameter '@id', which was not supplied”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM