Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#存储过程函数需要参数'@id';这是没有提供的_C#_Sql Server_Winforms - Fatal编程技术网

C#存储过程函数需要参数'@id';这是没有提供的

C#存储过程函数需要参数'@id';这是没有提供的,c#,sql-server,winforms,C#,Sql Server,Winforms,我得到了这个错误,虽然我知道它被问了很多次,我试图研究我的错误,但仍然没有任何工作 我的代码有错误吗?我希望有人能在这件事上提供帮助。这件事我已经坚持了一个星期了。我尝试了其他开发者的不同意见,但仍然没有解决任何问题 存储过程: ALTER PROCEDURE dbo.AddProduct @ID int, @Image varbinary(max), @Supplier varchar(50), @Codeitem varchar(50), @Itemdescription varchar(

我得到了这个错误,虽然我知道它被问了很多次,我试图研究我的错误,但仍然没有任何工作

我的代码有错误吗?我希望有人能在这件事上提供帮助。这件事我已经坚持了一个星期了。我尝试了其他开发者的不同意见,但仍然没有解决任何问题

存储过程:

ALTER PROCEDURE dbo.AddProduct

@ID int,
@Image varbinary(max),
@Supplier varchar(50),
@Codeitem varchar(50),
@Itemdescription varchar(50),   
@Date varchar(50),
@Quantity varchar(50),
@Unitcost varchar(50),
@ADD nvarchar(50)

AS
BEGIN
        IF @ADD = 'Insert'
INSERT INTO  dbo.employee_product
(
Image,
Supplier,
Codeitem,
Itemdescription,
Date,
Quantity,
Unitcost    
)
VALUES
(
@Image,
@Supplier,
@Codeitem,
@Itemdescription,
@Date,
@Quantity,
@Unitcost   
)

END
        byte[] image = null;
        var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
        var read = new BinaryReader(stream);
        image = read.ReadBytes((int)stream.Length);
        using (var con = SQLConnection.GetConnection())
        {


            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text))
            {
                MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                var selectCommand = new SqlCommand("AddProduct", con);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.AddWithValue("@ADD", "Insert");
                selectCommand.Parameters.AddWithValue("@ID",SqlDbType.Int ).Value = 0;
                selectCommand.Parameters.AddWithValue("@Image", image);
                selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
                selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Date", txt_date.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
                selectCommand.ExecuteNonQuery();
                MessageBox.Show("Added successfully", "SIMS", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                _view._product();

            }

        }
我的代码:

ALTER PROCEDURE dbo.AddProduct

@ID int,
@Image varbinary(max),
@Supplier varchar(50),
@Codeitem varchar(50),
@Itemdescription varchar(50),   
@Date varchar(50),
@Quantity varchar(50),
@Unitcost varchar(50),
@ADD nvarchar(50)

AS
BEGIN
        IF @ADD = 'Insert'
INSERT INTO  dbo.employee_product
(
Image,
Supplier,
Codeitem,
Itemdescription,
Date,
Quantity,
Unitcost    
)
VALUES
(
@Image,
@Supplier,
@Codeitem,
@Itemdescription,
@Date,
@Quantity,
@Unitcost   
)

END
        byte[] image = null;
        var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
        var read = new BinaryReader(stream);
        image = read.ReadBytes((int)stream.Length);
        using (var con = SQLConnection.GetConnection())
        {


            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text))
            {
                MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                var selectCommand = new SqlCommand("AddProduct", con);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.AddWithValue("@ADD", "Insert");
                selectCommand.Parameters.AddWithValue("@ID",SqlDbType.Int ).Value = 0;
                selectCommand.Parameters.AddWithValue("@Image", image);
                selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
                selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Date", txt_date.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
                selectCommand.ExecuteNonQuery();
                MessageBox.Show("Added successfully", "SIMS", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                _view._product();

            }

        }

您在C#代码中添加了十个参数:

var selectCommand = new SqlCommand("AddProduct", con);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@ADD", "Insert");
selectCommand.Parameters.AddWithValue("@ID",0);
selectCommand.Parameters.AddWithValue("@Image", image);
selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text.Trim()); // <== duplicate?
selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
selectCommand.Parameters.AddWithValue("@Date", txt_date.Text.Trim());
selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
var selectCommand=newsqlcommand(“AddProduct”,con);
选择Command.CommandType=CommandType.StoredProcess;
选择Command.Parameters.AddWithValue(“@ADD”,“Insert”);
选择Command.Parameters.AddWithValue(“@ID”,0);
选择Command.Parameters.AddWithValue(“@Image”,Image);
选择Command.Parameters.AddWithValue(“@Supplier”,cbox\u Supplier.Text);

选择Command.Parameters.AddWithValue(“@Supplier”,cbox_Supplier.Text.Trim());// 您在C#代码中添加了十个参数:

var selectCommand = new SqlCommand("AddProduct", con);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@ADD", "Insert");
selectCommand.Parameters.AddWithValue("@ID",0);
selectCommand.Parameters.AddWithValue("@Image", image);
selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text.Trim()); // <== duplicate?
selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
selectCommand.Parameters.AddWithValue("@Date", txt_date.Text.Trim());
selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
var selectCommand=newsqlcommand(“AddProduct”,con);
选择Command.CommandType=CommandType.StoredProcess;
选择Command.Parameters.AddWithValue(“@ADD”,“Insert”);
选择Command.Parameters.AddWithValue(“@ID”,0);
选择Command.Parameters.AddWithValue(“@Image”,Image);
选择Command.Parameters.AddWithValue(“@Supplier”,cbox\u Supplier.Text);
选择Command.Parameters.AddWithValue(“@Supplier”,cbox_Supplier.Text.Trim());// 将一个问题读到另一个问题,它指出:

使用SqlParameterCollection的此重载时请小心。将方法添加到 指定整型参数值。因为此重载采用Object类型的值, 当值为零时,必须将整数值转换为对象类型,如 下面的C#示例演示

如果不执行此转换,编译器将假定您正在尝试调用 SqlParameterCollection.Add(string,SqlDbType)重载

这很可能是您的问题。

阅读一篇文章时,会提到另一个问题:

使用SqlParameterCollection的此重载时请小心。将方法添加到 指定整型参数值。因为此重载采用Object类型的值, 当值为零时,必须将整数值转换为对象类型,如 下面的C#示例演示

如果不执行此转换,编译器将假定您正在尝试调用 SqlParameterCollection.Add(string,SqlDbType)重载



这很可能是您的问题。

您添加了两次
@Supplier
。您应该阅读此内容。您有许多数据类型在这里似乎是一个糟糕的选择。@Steve但由于它们使用的是存储过程,AddWithValue不必猜测这些类型。不过,我还是喜欢直截了当地说。:)可能是@Miracle why why在地球上给我们看了一段与所贴图片无关的代码。在该图像中,没有向DataAdapter SelectCommand添加任何参数。您添加了两次供应商。您应该阅读此内容。您有许多数据类型在这里似乎是一个糟糕的选择。@Steve但由于它们使用的是存储过程,AddWithValue不必猜测这些类型。不过,我还是喜欢直截了当地说。:)可能是@Miracle why why在地球上给我们看了一段与所贴图片无关的代码。在该图像中,没有向DataAdapter SelectCommand添加任何参数。已将其删除,先生,很抱歉,但仍然出现错误。您可以运行SQL Profiler跟踪以查看实际发送到服务器的内容吗?第一次听说它已将其删除,先生,很抱歉,但是仍然有错误。您可以运行SQL Profiler跟踪以查看实际发送到服务器的内容吗?第一次听说它,但我已经将数据类型指定为int,并且已经增加了它。仍然需要将其转换为整数?不确定AddWithValue是否存在相同的问题。在任何情况下,最好调用Add(name,type).Value=0;只有一种方法能找到答案。我试过你的答案,但没有成功work@Miracle这是错误的。将方法名称更改为
Add
而不是
AddWithValue
,但我已经将数据类型指定为int,并已将其递增。仍然需要将其转换为整数?不确定AddWithValue是否存在相同的问题。在任何情况下,最好调用Add(name,type).Value=0;只有一种方法能找到答案。我试过你的答案,但没有成功work@Miracle这是错误的。将方法名称更改为
Add
not
AddWithValue