2017-09-09 65 views
-1

當我使用這段代碼時,返回null。但是當我試圖通過sql使用查詢時,我得到了正常的結果。
這裏是我的代碼:SQL C#:沒有從Result.Read返回()

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]; 
      } 
     } 

字節[500]被返回。有什麼方法可以解決它嗎? 謝謝!

+1

我不認爲應該有單引號括起來@ID變量。 – w0051977

+0

嘗試刪除@ name和@ id處的引號。 –

+0

@ jason.kaisersmith 爲什麼?當您使用字符串時,您需要使用引號 – Roy

回答

0

我相信你的SQL語句應該是這樣的:

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

,你的參數應該被添加到集合是這樣的:

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

你需要在mysql中使用字符串時添加引號 – Roy