将值从下拉列表和文本框插入数据库

时间:2016-08-22 08:54:37

标签: c# mysql sql asp.net

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

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();

1 个答案:

答案 0 :(得分:1)

您需要使用标量属性名称

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