为什么此代码不起作用我试图从文本框表和专业,从下拉列表中插入学生ID和学生姓名

int sID = Convert.ToInt32(TextBox1.Text);
string Name = TextBox2.Text;
var mjr = DropDownList1.SelectedItem.Value;
var crs = DropDownList2.SelectedItem.Text;

string registered = "insert into Registered (Student_name, StudentID, Major, Course) values(@sName ,@SID, @major , @course)";

while (reader.Read())
{
    ints.Add(reader.GetInt32(0));

    if (a.Count() <= ints.Count() && !a.Except(ints).Any())
    {
        SqlCommand com4 = new SqlCommand(registered, con);

        com4.Parameters.AddWithValue("sName", Name);
        com4.Parameters.AddWithValue("SID", sID);
        com4.Parameters.AddWithValue("major", mjr);
        com4.Parameters.AddWithValue("course", crs);
        com4.ExecuteNonQuery();

最佳答案

您需要添加带有标量属性名称的“@”

 com4.Parameters.AddWithValue("@sName", Name);
 com4.Parameters.AddWithValue("@SID", sID);
 com4.Parameters.AddWithValue("@major", mjr);
 com4.Parameters.AddWithValue("@course", crs);
 com4.ExecuteNonQuery();

10-08 00:21