使用占位符时无法对数据库执行更新

时间:2014-04-13 21:11:13

标签: c# mysql wpf

当我对更新的语法进行硬编码时,我能够对我的数据库进行更新,但是当我尝试使用占位符来准备我的语句时,没有任何反应。请告诉我我做错了什么。谢谢。

public void UpdatePrepare(string statement, string[] keys, string[] values)
        {
            MySqlConnection conn;
            //Declare and Instantiate the MySqlCommand object 
            MySqlCommand cmd = new MySqlCommand();

            //Assign the database connection to it 
            cmd.Connection = conn;


            cmd.CommandText = statement;

            //Call the prepare method on the command 
            cmd.Prepare();

            try
            {

                for (int x = 0; x < values.Length; x++)
                {
                    cmd.Parameters.AddWithValue(keys[x], values[x]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Error at for loop to move values to keys");
            }

            //Execute the command 
            cmd.ExecuteNonQuery(); 
        }

private void Button_Update(object sender, RoutedEventArgs e) 
        {
            string update =    "UPDATE @table SET @attr=@input WHERE @attr=@current;";//not working
            //string update = "UPDATE Missions SET Mission_Name='mission202' WHERE Mission_Name='mission101'"; //works

            string table = "Missions";
            string attr = "Mission_Name";
            string current = "'mission101'";
            string input = "'mission202'";

            string[] keys = new string[] {"@table", "@attr" , "@input", "@current"};
            string[] values = new string[] {table, attr, input, current}; 

            UpdatePrepare(update, keys, values);
            MessageBox.Show("Update Successful.");
        }
  

错误讯息:

     

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   ''任务'附近'SET'Mission_Name'='\'mission202 \''WHERE

0 个答案:

没有答案