将数据插入MySQL数据库C#

时间:2014-09-16 19:25:05

标签: c# mysql sql database

我在Web服务器上有一个MySQL数据库,其中包含idPlayers,First Name,Surname,Age和Height列。用户在表单上的文本框中输入数据,然后单击按钮将数据插入到已有多行数据的数据库中。以下是我的代码:

Connection.cs中的方法类:

public string AddPlayer(int idPlayers, string FirstName, string Surname, int Age, int Height)
    {
        string result;
        using (var Connection = new MySqlConnection(ConnectionString))
        {

            Connection.Open();

                using (var command = new MySqlCommand(
                    "INSERT INTO Players(idPlayers, First Name, Surname, Age, Height) VALUES(@idPlayers, @First Name, @Surname, @Age, @Height)", Connection))
                    try
                    {
                        command.Parameters.AddWithValue("idPlayers", idPlayers);
                        command.Parameters.AddWithValue("First Name", FirstName);
                        command.Parameters.AddWithValue("Surname", Surname);
                        command.Parameters.AddWithValue("Age", Age);
                        command.Parameters.AddWithValue("Height", Height);
                        command.ExecuteNonQuery();
                        Connection.Close();
                    }
                    catch
                    {
                        result = "Failure";
                        return result;
                    }                    
            }
        result = "Success";
        return result;  
        }       
    }

Form1.cs Class中的方法调用

public void button1_Click(object sender, EventArgs e)
    {
        string result1;
        int idPlayers = 3;
        String FirstName = FirstNameBox.Text;
        String Surname = SurnameBox.Text;
        int Age = Convert.ToInt32(AgeBox.Text);
        int Height = Convert.ToInt32(HeightBox.Text);

        Connection NewCon = new Connection();
        result1 = NewCon.AddPlayer(idPlayers, FirstName, Surname, Age, Height);
        MessageBox.Show(result1);

    }

当我单击按钮插入数据时,消息框显示Failure。您有什么想法为什么数据不会插入? ConnectionString绝对正确,因为我可以从我的程序中的其他地方读取数据库。

感谢您的帮助, 乔治

修订的Connection.cs类:

public string AddPlayer(int idPlayers, string FirstName, string Surname, int Age, int Height)
    {
        string result;
        using (var Connection = new MySqlConnection(ConnectionString))
        {

            Connection.Open();

                using (var command = new MySqlCommand(
                    "INSERT INTO Players(idPlayers, First Name, Surname, Age, Height) VALUES(@idPlayers, @FirstName, @Surname, @Age, @Height)", Connection))
                    try
                    {
                        command.Parameters.AddWithValue("@idPlayers", idPlayers);
                        command.Parameters.AddWithValue("@FirstName", FirstName);
                        command.Parameters.AddWithValue("@Surname", Surname);
                        command.Parameters.AddWithValue("@Age", Age);
                        command.Parameters.AddWithValue("@Height", Height);
                        command.ExecuteNonQuery();
                        Connection.Close();
                    }
                    catch(Exception ex)
                    {
                        result = ex.Message;
                        return result;
                    }                    
            }
        result = "Success";
        return result;  
        }       
    }

1 个答案:

答案 0 :(得分:1)

第一个问题,参数名称(通常是标识符)在查询中不能有空格:

"... VALUES(@idPlayers, @First Name, @Surname, @Age, @Height)"

查询引擎无法解释@First Name。用一个词:

"... VALUES(@idPlayers, @FirstName, @Surname, @Age, @Height)"

编辑:与此相关,您还在另一个标识符中使用空格:

"INSERT INTO Players(idPlayers, First Name, Surname, Age, Height) ..."

同样的问题,你不能使用这样的空格。我高度建议不要在列/表名称中使用空格。但是,如果您必须,那么您需要附上其标识符以在查询中引用它们。如果我没记错的话,MySQL会使用反向标记:

"INSERT INTO Players(idPlayers, `First Name`, Surname, Age, Height) ..."

第二个问题,您未正确添加参数:

command.Parameters.AddWithValue("idPlayers", idPlayers);

您没有名为idPlayers的参数,您有一个名为@idPlayers的参数。有区别。试试这个:

command.Parameters.AddWithValue("@idPlayers", idPlayers);

最大问题,您忽略了异常。不要忽略错误消息,他们会告诉你完全错误。您应该检查该异常:

catch (Exception ex)
{
    // "ex" contains the error information you're looking for
    // log it, show it to the user, do something with it
}