投稿 资料上传 提问 搜索
您现在的位置是: 首页 > 问答 > 正文

vs使用insert添加数据提示对象名无效是什么原因?

private void btnAdd_Click(object sender, EventArgs e){
string question = txtQuestion.Text;
string answer ="";
switch(answer) {
case "A":rdoA.Checked=true;break;
case "B":rdoB.Checked=true;break;
case "C":rdoC.Checked=true;break;
case "D":rdoD.Checked=true;break;
}
int difficulty = 0;
switch (difficulty) {
case 1: rdoA.Checked = true; break;
case 2: rdoB.Checked = true; break;
case 3: rdoC.Checked = true; break;
}
int subjectId = GetSubjectId(cboCourse.Text);
string optionA = txtA.Text;
string optionB = txtB.Text;
string optionC = txtC.Text;
string optionD = txtD.Text;
string sql = string.Format(@"insert into Qusetion (Question,Answer,Difficulty,SubjectId,OptionA,OptionB,OptionC,OptionD)
values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}')",
question,answer,difficulty,subjectId,optionA,optionB,optionC,optionD);
try{
SqlCommand comm = new SqlCommand(sql, DBHelper.conn);
DBHelper.conn.Open();
int num = comm.ExecuteNonQuery();
if (num > 0){
MessageBox.Show("添加数据成功!");
}else{ MessageBox.Show("添加数据失败!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.conn.Close();
}


问题回复
  1. 程序猿
    程序猿IT技术之家

    string sql = string.Format(@"insert into Qusetion (Question,Answer,Difficulty,SubjectId,OptionA,OptionB,OptionC,OptionD)
    values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}')",
    请检查一下你的的表名、字段名、字段个数、类型和这里sql的是否匹配  

    回复