不能用这个字符串更新数据库我做错了什么?

时间:2015-03-27 13:22:41

标签: c# mysql

这是代码,任何人都可以帮助我吗?

Value = Convert.ToInt32(Console.ReadLine());

switch (Value)
{
    case 1:
        Console.WriteLine("Please enter number");
        number = Convert.ToInt32(Console.ReadLine());
         Console.WriteLine("Please insert the first word");
        word1 = Convert.ToString(Console.ReadLine());
        Console.WriteLine("Please insert the second word");
        word2 = Convert.ToString(Console.ReadLine());
        string sql1 = "INSERT INTO storage ('" + number + "', '" + word1 + "', '" + word2 + "')";


        one.Update(sql1);


        break;

1 个答案:

答案 0 :(得分:0)

我会通过以下方式解决:

替换

string sql1 = "INSERT INTO storage ('" + number + "', '" + word1 + "', '" + word2 + "')";
one.Update(sql1);

string sCommand = "INSERT INTO storage (@number, @word1, @word2)";
using (MySqlConnection mConnection = new MySqlConnection(ConnectionString))
{
    mConnection.Open();
    using (MySqlCommand myCmd = new MySqlCommand(sCommand, mConnection))
    {
        myCmd.Parameters.Add(new MySqlParameter("@number", number));
        myCmd.Parameters.Add(new MySqlParameter("@word1", word1));
        myCmd.Parameters.Add(new MySqlParameter("@word2", word2));
        myCmd.ExecuteNonQuery();
    }
}

...或提供有关SimpleDataScource-Object

的更多信息