C# 将项目插入数据库

C# 将项目插入数据库,c#,.net,winforms,combobox,C#,.net,Winforms,Combobox,我对insert into语句有问题 cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,Position,Photo) " + "values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text

我对insert into语句有问题

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");
错误:语法错误插入到


有人能给我建议吗?请为我糟糕的英语语法感到抱歉。

您的查询中似乎遗漏了一个参数,请尝试使用此参数

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

您没有在代码中添加@Photo参数

这对于测试来说是可以的,但您不应该以这种方式插入数据库。这会使您的系统暴露在一个错误的环境中。您应该尽可能使用参数化查询。差不多

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参数是位置参数,这意味着您必须 按照查询中的确切顺序指定它们


旁注:您在使用
位置的参数方面做得很好,为什么您不在
id
中使用参数?“有错误”->什么错误?是的,就像@Ic所说的,使用参数化查询,并且,您能告诉我们错误吗?组合框是否返回值?您首先绑定的是正确的吗?@lc.,等等,我打开我的笔记本电脑我的代码怎么了?我在下面有一个photo的编码,但我从来没有把它放在这里。您在SQL查询中指定photo,但没有绑定参数。@ThamJunKai请勾选我的答案:)
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");