插入语句时遇到问题..
cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,Position,Photo) " +
"values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text +
"','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text +
"','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);
conv_photo();
cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedValue);
con.Open();
int n = cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Inserted");
loaddata();
rno++;
}
else
MessageBox.Show("No Insert");
错误:语法错误INSERT INTO
有人可以建议我吗?对不起,我的英语语法不好。
答案 0 :(得分:1)
好像你错过了查询中的参数,请尝试使用此
cmd.CommandText = "insert into Table1 (id,Position) values (@id,@Position)";
cmd.parameters.addwithvalue("@id", textBox1.Text);
cmd.parameters.addwithvalue("@Position", combobox1.selectedvalue);
新更新 - 位置是oleh db保留字,尝试更改此查询,将封面置于如下位置
cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,[Position],Photo) " +
"values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text +
"','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text +
"','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);
答案 1 :(得分:0)
您错过了在代码中添加@Photo参数。
这对于测试目的是可行的,但是你不应该以这种方式插入数据库。这会使您的系统暴露于SQL Injection。您应该尽可能使用参数化查询。像
这样的东西int result=0;
using (OleDbConnection myConnection = new OleDbConnection ("YourConnectionString"))
{
cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,Position,Photo) values (@ID, @Gender, @DateOfBirth, @Race, @WorkingPlace, @PassportNO, @DateOfExpire, @Position, @Photo)", con);
conv_photo();
cmd.Parameters.AddWithValue("@ID", textBox5.Text);
// Specify all parameters like this
try
{
con.Open();
result = Convert.ToInt32(cmd.ExecuteNonQuery());
}
catch( OledbException ex)
{
// Log error
}
finally
{
if (con!=null) con.Close();
}
}
if(result > 0)
// Show success message
另请注意,OleDb参数是位置的,意味着你必须这样做 按照查询中的确切顺序指定它们。 OleDbParameter Class (MSDN)
答案 2 :(得分:0)
There is no value for parameter @Photo, and if your photo field is not nullable or empty
in database structure then how you can add null value in that.So make your data field
nullable or pass value to parameter @Photo.I think it will solve your problem.
cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,Position,Photo) " +
"values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text +
"','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text +
"','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);
conv_photo();
cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedValue);
cmd.Parameters.AddWithValue("@Photo", assignvalue);
con.Open();
int n = cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Inserted");
loaddata();
rno++;
}
else
MessageBox.Show("No Insert");