2017-01-05 99 views
-1

任何人都可以解釋我如何使用executePreparedQuery? 我想將變量填充到我的MySql數據庫中。執行準備好的查詢c#mysql

收到的一些錯誤使用的:

API.exported.database.executePreparedQuery("INSERT INTO users (ID,username,password) VALUES('','testing3',pass)","pass","password123456"); 

public void executePreparedQuery(string sql, Dictionary<string, string> parameters) 
{ 
    using (MySqlConnection conn = new MySqlConnection(connStr)) 
    { 
     try 
     { 
      MySqlCommand cmd = new MySqlCommand(sql, conn); 
      conn.Open(); 
      foreach (KeyValuePair<string, string> entry in parameters) 
      { 
       cmd.Parameters.AddWithValue(entry.Key, entry.Value); 
      } 
      cmd.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      API.consoleOutput("DATABASE: [ERROR] " + ex.ToString()); 
     } 
    } 
} 

謝謝:)

+0

它不會emn你傳遞一個字典作爲方法的參數 – NicoRiff

+0

好的,你能給我一個API.exported.database.executePreparedQuery的例子嗎? – Florian

回答

0

你要通過正確的字典到executePreparedQuery方法:

Dictionary<string, string> dic = new Dictionary<string, string>(); 
dic.Add("pass", "password123456"); 

API.exported.database.executePreparedQuery("INSERT INTO users (ID,username,password) VALUES('','testing3',pass)",dic); 
+0

謝謝,但得到了: MySql.Data.MySqlClient.MySqlException(0x80004005):在'字段列表'中的未知列'傳遞' – Florian

+0

將「傳遞」更改爲「?傳遞」,它工作。謝謝 :) – Florian