SQL C#:Result.Read() 没有返回任何内容
SQL C#: Nothing gets returned from Result.Read()
当我使用这段代码时,我返回了 null。但是当我尝试使用 sql 的查询时,我得到了正常的结果。
这是我的代码:
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]。有什么办法可以解决吗?
谢谢!
我相信您的 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);
当我使用这段代码时,我返回了 null。但是当我尝试使用 sql 的查询时,我得到了正常的结果。
这是我的代码:
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]。有什么办法可以解决吗? 谢谢!
我相信您的 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);