0

我在插入语句时遇到问题..

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

错误:语法错误插入

谁能给我建议?拜托,对不起我的英语语法不好。

4

3 回答 3

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);
于 2013-03-18T03:25:04.350 回答
0

您错过了在代码中添加 @Photo 参数。

这对于测试目的是可以的,但你不应该以这种方式插入数据库。这会将您的系统暴露给SQL 注入。您应该尽可能使用参数化查询。就像是

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 类 (MSDN)

于 2013-03-18T05:03:54.733 回答
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");
于 2013-03-18T12:13:14.737 回答