数据未出现在SQL数据库中

时间:2019-03-14 11:13:35

标签: c# sql

我正在尝试将数据INSERT放入我的SQL数据库中,但是根本没有显示任何内容。

这是一款在线角色扮演游戏。没有错误,但是当我使用phpmyadminXAMP刷新浏览器时,没有数据显示。

MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            string checkDatabase = "select * from players where username = @playerName";
            MySqlCommand command = new MySqlCommand(checkDatabase, connection);
            command.Parameters.AddWithValue("@playerName", player.SocialClubName);

            MySqlDataReader reader = command.ExecuteReader();

            if(reader.Read())
            {
                player.SendChatMessage("There is an account with the assiociated Social Club Profile!");
            }
            else
            {

                    MySqlConnection connection1 = new MySqlConnection(connectionString);
                    connection1.Open();
                    string playerInsert = "insert into players(username,password) VALUES (@user,@password)";
                    MySqlCommand command1 = new MySqlCommand(playerInsert, connection1);
                    command1.Parameters.AddWithValue("@user", player.SocialClubName);
                    command1.Parameters.AddWithValue("@password", password);
                    connection1.Close();

            }

            connection.Close();

2 个答案:

答案 0 :(得分:4)

您需要执行查询。试试:

...
command1.ExecuteNonQuery();

答案 1 :(得分:3)

这是因为您根本不执行查询,如下面发布的代码所示

             string playerInsert = "insert into players(username,password) VALUES (@user,@password)";
                MySqlCommand command1 = new MySqlCommand(playerInsert, connection1);
                command1.Parameters.AddWithValue("@user", player.SocialClubName);
                command1.Parameters.AddWithValue("@password", password);
                command1.ExecuteNonQuery(); //execute the query
                connection1.Close();