Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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# 必须声明标量变量“@Stockno”_C# - Fatal编程技术网

C# 必须声明标量变量“@Stockno”

C# 必须声明标量变量“@Stockno”,c#,C#,我有两种不同形式的addstock表单和viewstocks表单 当我在addstock表单中添加库存号和详细信息并返回时,请按viewstock表单中的刷新按钮;新记录将显示在viewstock表单中 viewstock表单还具有“删除”按钮 当我选择行并按delete键时,会出现此错误 必须声明标量变量@Stockno 我需要打电话。请帮忙 private void bunifuFlatButton3_Click(object sender, EventArgs e) { try

我有两种不同形式的addstock表单和viewstocks表单

当我在addstock表单中添加库存号和详细信息并返回时,请按viewstock表单中的刷新按钮;新记录将显示在viewstock表单中

viewstock表单还具有“删除”按钮

当我选择行并按delete键时,会出现此错误

必须声明标量变量@Stockno

我需要打电话。请帮忙

private void bunifuFlatButton3_Click(object sender, EventArgs e)
{
    try 
    {
        conn.Close();
        conn.Open();

        String DeleteQuery = "delete from Stocks_Item where Stock_code = (@Stockno)";

        SqlDataAdapter execute = new SqlDataAdapter(DeleteQuery, conn);
        execute.SelectCommand.ExecuteNonQuery();

        MessageBox.Show("You've deleted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

        conn.Close();
        cmd.ExecuteNonQuery();

        SqlDataAdapter data = new SqlDataAdapter("Select * from Stocks_Item", conn);
        DataTable dt = new DataTable();
        data.Fill(dt);

        dataGridView1.DataSource = dt;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
要删除某些内容时,不应使用SqlDataAdapter—使用独立的SqlCommand—如下所示:

private void bunifuFlatButton3_Click(object sender, EventArgs e)
{
    try 
    {
        conn.Close();

        string DeleteQuery = "delete from Stocks_Item where Stock_code = @Stockno";

        SqlCommand cmdDelete = new SqlCommand(DeleteQuery, conn);
        -- add the parameter!
        cmdDelete.Parameters.Add("@stockno", SqlDbType.Int).Value = (provide the value for `@stockno` here!);

        // open connection, execute command, close connection
        conn.Open();
        cmdDelete.ExecuteNonQuery();
        conn.Close();

        MessageBox.Show("You've deleted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        .....
    }
}

您甚至还没有在删除中添加要比较的实际值。我觉得你写的代码你并不完全理解。