2017-06-20 76 views
0

我已經編寫了一個程序來處理XML並將其導入到Accsess表中,但它會產生錯誤。我有一個sql注入類型的聲明我認爲這不太對,一些如何。如何將Xml數據添加到Accsess數據庫錯誤

The Database(Accsess) has this 
Index Field 1 Autonumber primary key 
Property Field 2 ShortText 
PValue Field 3 ShortText 
PDefault Field 4 ShortText 
Ptype Field 5 ShortText 

我的代碼產生這個錯誤 Error System.Data.OleDb.OleDbException (0x80040E10): Parameter ?_1 has no default value. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at Beta3.Form1.button1_Click(Object sender, EventArgs e) in C:\Users

An this is the code that produces it. 

String files; 
     files = "c:\\temp\\launch.xml"; 
     SetCon.Text = SetCon.Text + "Processing Lauch Working \n"; 
     String item1, item2, item3, item4; 
     XmlReader reader1 = XmlReader.Create(files); 

     while (reader1.Read()) 
     { 
      item1 = reader1.GetAttribute("name"); 
      item2 = reader1.GetAttribute("amount"); 
      item3 = reader1.GetAttribute("default"); 
      item4 = reader1.GetAttribute("group"); 
      try 
      { 
       SetCon.Text = SetCon.Text + "Adding Records \n"; 
       string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False"); 
       string cmdText = "INSERT INTO Launch([Property], [PValue], [PDefault],[PType]) VALUES(?,?,?,?)"; 
       using (OleDbConnection Conn = new OleDbConnection(ConnString)) 
       { 
        using (OleDbCommand cmd = new OleDbCommand(cmdText, Conn)) 
        { 

         cmd.Parameters.AddWithValue("Property", item1); 
         cmd.Parameters.AddWithValue("PValue", item2); 
         cmd.Parameters.AddWithValue("PDefault", item3); 
         cmd.Parameters.AddWithValue("PType", item4); 

         Conn.Open(); 
         cmd.ExecuteNonQuery(); 
         Conn.Close(); 
        } 

       } 

我認爲它的自動編號導致錯誤等,但是蔭沒有足夠的經驗去了解它。可能有些人可以發現Iam做錯了什麼。

回答

1

您可以嘗試使用@而不是?

試試這個:

string cmdText = "INSERT INTO Launch([Property], [PValue], [PDefault],[PType]) VALUES('@Property','@PValue','@PDefault','@PType')"; 
       using (OleDbConnection Conn = new OleDbConnection(ConnString)) 
       { 
        using (OleDbCommand cmd = new OleDbCommand(cmdText, Conn)) 
        { 

         cmd.Parameters.AddWithValue("@Property", item1); 
         cmd.Parameters.AddWithValue("@PValue", item2); 
         cmd.Parameters.AddWithValue("@PDefault", item3); 
         cmd.Parameters.AddWithValue("@PType", item4); 

         Conn.Open(); 
         cmd.ExecuteNonQuery(); 
         Conn.Close(); 
        } 

       } 
+0

錯誤System.Data.OleDb.OleDbException(0x80040E10):參數@沒有默認值。 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult小時) 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,對象&的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteCommandText(對象&的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Object&executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method) at System.Data.OleDb。 – Data

+0

@Data我已編輯我的答案 – ohadinho

+1

本示例處理編輯後的示例,但正如我發現的那樣如果添加代碼(複製粘貼),那麼它只會將文本item1,item2 ..填充到columes/records中。 。它沒有放入實際的變量值。 @ ohadinho Plz檢查你的代碼爲Samams編碼exable表 – Data