簡體   English   中英

需要未提供的參數“@id”。 id 是主鍵和身份

[英]expects parameter '@id', which was not supplied. the id is primary key and identity

我收到此錯誤:

需要未提供的參數“@id”。 id 是主鍵和身份

我的代碼:

namespace se_up_de_in
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-R0N4ID3;Initial Catalog=DBTask2;Integrated Security=True");

        SqlCommand comm;
        SqlDataAdapter Adapter;

        DataTable Table = new DataTable();

        private void button2_Click(object sender, EventArgs e)
        {
            using (comm = new SqlCommand("proc_insertid", conn))
            {
                comm.CommandType = CommandType.StoredProcedure;
                conn.Open();

                SqlParameter[] parameter = new SqlParameter[5];
                parameter[0] = new SqlParameter("@id", SqlDbType.Int);
                parameter[0].Value = textBox5.Text;

                parameter[1] = new SqlParameter("@username", SqlDbType.NChar);
                parameter[1].Value = textBox1.Text;

                parameter[2] = new SqlParameter("@password", SqlDbType.NChar);
                parameter[2].Value = textBox2.Text;


                parameter[3] = new SqlParameter("@email", SqlDbType.NVarChar);
                parameter[3].Value = textBox3.Text;


                parameter[4] = new SqlParameter("@Type", SqlDbType.NChar);
                parameter[4].Value = textBox4.Text;

                comm.ExecuteNonQuery();

                comm.Parameters.AddRange(parameter);

                conn.Close();

                dataGridviewfill();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (comm = new SqlCommand("proc_Dlelete", conn))
            {
                comm.CommandType = CommandType.StoredProcedure;
                conn.Open();

                SqlParameter param = new SqlParameter();
                param = new SqlParameter("@id", SqlDbType.NChar);
                param.Value = textBox5.Text;

                comm.ExecuteNonQuery();
                comm.Parameters.Add(param);

                conn.Close();

                dataGridviewfill();
            }
        }
    }
}

我的存儲過程:

CREATE PROCEDURE proc_insert
     (@id INT,
      @username NCHAR(10),
      @password NCHAR(10),
      @email NVARCHAR(MAX),
      @Type NCHAR(10))
AS
BEGIN
    INSERT INTO ttask2 (id, username, password, email, Type)
    VALUES (@id, @username, @password, @email, @Type)
END
comm.ExecuteNonQuery();
comm.Parameters.AddRange(parameter);

顛倒這些; 您需要在執行命令之前添加參數。

comm.Parameters.AddRange(parameter);
comm.ExecuteNonQuery();

(與您使用命令/參數的其他地方相同)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM