SQL C#: Nothing gets returned from Result.Read()

54 Views Asked by At

When I use this code, I get null returned. But when I am trying to use the query by sql I get normal result.
Here is my code:

public byte[] GetInfo(UnturnedPlayer player , string vehiclename)
        {
            try
            {
                MySqlConnection connection = createConnection();
                MySqlCommand command = connection.CreateCommand();
                command.CommandText = "select `info` from `" + GaragePlugin.Instance.Configuration.Instance.DatabaseTableName + "` where `player` = '@id' AND `vname` = '@name';";
                connection.Open();
                command.Parameters.AddWithValue("@id", player.CSteamID);
                command.Parameters.AddWithValue("@name", vehiclename);
                Console.WriteLine(command.CommandText.Replace("@id", player.CSteamID.ToString()).Replace("@name", vehiclename));
                var result = command.ExecuteScalar();
                if(result != null)
                {
                    Console.WriteLine(result.ToString(), ConsoleColor.Blue);
                    byte[] bytearray = Convert.FromBase64String(result.ToString());
                    return bytearray;
                }
                connection.Close();
                return new byte[500];
            }
            catch (Exception ex)
            {
                Logger.Log("Error with GetInfo: " + ex);
                return new byte[500];
            }
        }

Byte[500] gets returned. Is there any way to fix it? Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

I believe your SQL statement should look like this:

command.CommandText = "select info from " + GaragePlugin.Instance.Configuration.Instance.DatabaseTableName + " where player = @id AND vname = @name;

and your parameters should be added to the collection like this:

command.Parameters.AddWithValue(@id, player.CSteamID);
command.Parameters.AddWithValue(@name, vehiclename);