执行准备好的查询c#mysql

时间:2017-01-05 19:18:37

标签: c# mysql variables

谁能解释我如何使用executePreparedQuery? 我想将变量填充到我的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());
        }
    }
}

谢谢:)

1 个答案:

答案 0 :(得分: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);