2013-03-18 61 views
0

我有一個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"); 

錯誤:語法錯誤INSERT INTO

任何人都可以告訴我?請對不起,因爲我的英語語法不好。

+1

邊評論:你已經做使用'Position',你怎麼不使用一個'id'參數的好工作呢? – 2013-03-18 03:21:43

+2

「有錯誤」 - >什麼錯誤? – 2013-03-18 03:22:03

+0

是像@Ic說的那樣,使用參數化查詢,而且,你能給我們提供錯誤嗎?組合框是否返回值?你是否正確地綁定它? – 2013-03-18 03:23:42

回答

1

看起來像你錯過了在您的查詢參數,請嘗試使用此

cmd.CommandText = "insert into Table1 (id,Position) values (@id,@Position)"; 

cmd.parameters.addwithvalue("@id", textBox1.Text); 
cmd.parameters.addwithvalue("@Position", combobox1.selectedvalue); 

新更新 -the位置的奧萊分貝保留字,嘗試此查詢的變化,把蓋到如下位置

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

嗯...我編輯的代碼... – 2013-03-18 03:46:57

+0

但仍然無法正常工作 – 2013-03-18 04:31:56

+0

@ThamJunKai最新代碼如何?它工作嗎? – 2013-03-18 07:01:00

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)

+0

我在下面有照片編碼,但我從來沒有把它放在這裏。 – 2013-03-18 08:55:56

+0

您在SQL查詢中指定了照片,但您沒有爲其綁定參數。 – 2013-03-18 09:07:55

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