【问题标题】:Insert item to database将项目插入数据库
【发布时间】:2013-03-18 03:18:45
【问题描述】:

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

ERROR : 语法错误 INSERT INTO

谁能给我建议?请原谅我的英语语法不好。

【问题讨论】:

  • 旁注:你用Position的参数做得很好,为什么id不用一个?
  • “有错误” -> 什么错误?
  • 是的,就像@Ic 说的那样,使用参数化查询,而且,你能给我们错误吗?组合框是否返回值?你一开始就正确绑定了吗?
  • @lc.,等一下,我打开我的笔记本电脑
  • 我的代码有什么问题?

标签: c# .net winforms combobox


【解决方案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);

【讨论】:

  • @ThamJunKai 请勾选我的答案 :)
【解决方案2】:

您错过了在代码中添加@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)

【讨论】:

  • 我在下面有照片的编码,但我从来没有放在这里。
  • 您在 SQL 查询中指定照片,但您没有为其绑定参数。
【解决方案3】:
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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多