2010-09-16 39 views
0

我有INSERT TO TABLE AG_MAS在FoxPro如下:INSERT在FoxPro

private void button1_Click(object sender, EventArgs e) 
    { 
     OleDbConnection connect = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=pl_prov.dbf"); 
     try 
     { 
      connect.Open(); 
      string strSQL = "insert into ag_mas"; 
      strSQL += "(ag_code,ag_level,ag_group,ag_rcode,ag_status,"; 
      strSQL += "ag_type,ag_gender,ag_lname,ag_fname,ag_add1,"; 
      strSQL += "ag_add2,ag_add3,ag_add4,ag_class,ag_vouch,"; 
      strSQL += "pl_prov,pl_zone,ag_jdate,ag_ddate,ag_dbrith,ag_bank ,"; 
      strSQL += "ag_bankno,ag_telno,ag_faxno,ag_email,ag_ppay,"; 
      strSQL += "ag_contno,ag_dcont,ag_ladate,ag_lpolno,ag_lendno,"; 
      strSQL += "ag_comm,ag_maxcom,ag_orcomm,ag_om1com,ag_om2com,"; 
      strSQL += "ag_om3com,ag_oy1com, ag_oy2com,ag_oy3com,ag_network)"; 
      strSQL += "values ("; 
      strSQL += "'" + textBox1.Text + "',";//ag_code 
      strSQL += "'" + textBox2.Text + "',";//ag_level 
      strSQL += "'" + textBox3.Text + "',";//ag_group 
      strSQL += "' ' ,";        //ag_rcode 
      strSQL += "'" + textBox4.Text + "',";//ag_status 
      strSQL += "'" + textBox22.Text + "',";//ag_type 
      strSQL += "'" + textBox5.Text + "',";//ag_gender 
      strSQL += "'" + textBox6.Text + "',";//ag_lname 
      strSQL += "'" + textBox7.Text + "',";//ag_fname 
      strSQL += "'" + textBox8.Text + "',";//ag_add1 
      strSQL += "'" + textBox9.Text + "',";//ag_add2 
      strSQL += "'" + comboBox1.Text + "',";//ag_add3 
      strSQL += "' ' ,";         //ag_add4 
      strSQL += "'" + textBox10.Text + "',";//ag_class 
      strSQL += "'" + textBox11.Text + "',";//ag_vouch 
      strSQL += "'" + comboBox1.SelectedValue + "',";//pl_prov 
      strSQL += "' ' ,";         //pl_zone 
      strSQL += "'" + textBox12.Text + "',"; //ag_jdate 
      strSQL += "'1987/10/10',";        //ag_ddate 
      strSQL += "'1987/10/10',";        //ag_dbirth 
      strSQL += "' ' ,";         //ag_bank 
      strSQL += "' ' ,";         //ag_bankno 
      strSQL += "' ' ,";         //ag_telno 
      strSQL += "' ' ,";         //ag_faxno 
      strSQL += "' ' ,";         //ag_email 
      strSQL += "'" + textBox13.Text + "',"; //ag_ppay 
      strSQL += "'" + textBox15.Text + "',";//ag_contno 
      strSQL += "'" + textBox14.Text + "',";//ag_dcont 
      strSQL += "'1987/10/10', ";    //ag_ladate 
      strSQL += "' ' ,";         //ag_lpopno 
      strSQL += "' ' ,";         //ag_lendno 
      strSQL += "0,";         //ag_comm 
      strSQL += "0,";         //ag_maxcom 
      strSQL += "0,";         //ag_orcomm 
      strSQL += "0,";         //ag_om1com 
      strSQL += "0,";         //ag_om2com 
      strSQL += "0,";         //ag_om3comm 
      strSQL += "0,";         //ag_oy1com 
      strSQL += "0,";         //ag_oy2Xcom 
      strSQL += "0,";         //ag_oy3com 
      strSQL += "' ')";         //ag_network 
      OleDbCommand cmd = new OleDbCommand(strSQL, connect); 
      cmd.ExecuteNonQuery(); 
     } 
     catch(Exception err) 
     { 
      MessageBox.Show(err.Message); 
     } 
    } 
} 

但我不能插入。因爲錯誤「數據錯失匹配」請幫助我。

回答

0

當您嘗試向字段中插入不適當的數據類型時,會發生數據類型不匹配錯誤。例如,如果您嘗試將字符串存儲到整數字段中。

乍一看,我認爲你的日期格式是問題。將「'1987/10/10'」改爲「{^ 1987-10-10}」。

如果更改日期並不能解決問題,那麼如果您可以向我顯示strSQL變量的結果和ag_mas表的結構將會很有幫助。

+0

非常感謝! – maolddv 2010-09-17 01:19:08

1

我同意弗蘭克在日期問題...它也可能試圖做一個文本到數字字段(或任何其他這樣的交換)。

但是,如果/當您使用真正的SQL數據庫(如Oracle,SQL-Server,MySQL等)時,可能會出現更大的問題......(並非我對VFP有任何反應,並且已經使用Foxpro編程自85年以來)但是像你這樣做SQL語句,特別是在任何基於Web的系統中,都會讓你對SQL注入攻擊敞開大門。

它更好地使用參數化查詢/插入/更新/刪除。

0

我同意弗蘭克關於解決方案,我相信DRapp有一個關於如何進行插入語句的有效觀點。爲了增加DRapp的建議,我想指出你可以使用LINQ to VFP來處理你所有的sql命令。