2015-10-16 99 views
1

當我插入數據MS Access數據庫。它是不是給數據庫沒有插入任何錯誤,但數據插入查詢在C#與MS Access數據庫

代碼:

private void btnsubmit_Click(object sender, EventArgs e) 
    { 

     int row = dataGridView1.RowCount; 
     for (int i = 0; i < row - 1; i++) 
     { 
      String str = "insert into JDS_Data(job_no,order_no,Revision,DesignSpec,Engine_Type,date,LE_IN_Designer,CPH_Designer,Exp_Del_Week,Action_code,Rev_Description,Ref_pattern,Name_of_mock_up,EPC_Drawing,Turbocharger_no_Type,Engine_Specific_Requirement,Draft_sketch_with_details,Air_cooler_type,Description_of_Job,SF_No,Standard,Prority_Sequence,Remark,Part_family,Modified_Date,User) values('" + txtjobno.Text + "','" + txtorderno.Text + "','" + txtrevison.Text + "','" + txtds.Text + "','" + txtenginetype.Text + "','" + dateTimePicker1.Text + "','" + txtleindesigner.Text + "','" + txtcphdesigner.Text + "','" + txtexpweek.Text + "','" + txtactioncode.Text + "','" + txtrevdescription.Text + "','" + txtrefpatern.Text + "','" + txtmockup.Text + "','" + txtepcdwg.Text + "','" + txtturbono.Text + "','" + txtenginereq.Text + "','" + txtdraft.Text + "','" + txtaircolertype.Text + "','" + txtdespjob.Text + "','" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[4].Value.ToString() + "','" + DateTime.Today + "','" + mdlconnection.user_name + "')"; 

      int dd = mdlconnection.excuteQuery(str); 
      MessageBox.Show(str); 
      //if (dd > 0) 
      { 
       MessageBox.Show("Data Saved Successfully..!!!"); 

      } 

     } 

    } 

代碼:

public static int excuteQuery(string q) 
    { 
     int d = 0; 
     try 
     { 
      OleDbCommand cmd = new OleDbCommand(q, con); 
      d = cmd.ExecuteNonQuery(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
     return d; 
    } 
+1

這是什麼'mdlconnection.excuteQuery'方法是什麼呢?你應該總是使用[參數化查詢](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。這種字符串連接對於[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊是開放的。 –

+0

用它下面的[edit]按鈕更新你的問題。 ExcuteQuery是:public static int excuteQuery(string q) { int d = 0; –

+0

嘗試 OleDbCommand cmd = new OleDbCommand(q,con); d = cmd.ExecuteNonQuery(); } catch(Exception e) { } Console.WriteLine(e.Message); } return d; } – shweta

回答

0

如果你正在使用DataContext(你一點信息提供) 你應該重寫你的語句來馬TCH的例子:

var customers = db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle, 
    Address, City, Region, PostalCode, Country, Phone, Fax 
    FROM dbo.Customers 
    WHERE City = {0}", "London"); 

我應該建議使用this tutorial for the connection不是實際