下面我使用的是MySqlDataReader,想知道如何循环遍历所有列名和值,并通过列名和列值的键和值将它们添加到<string,string>
字典中?我的代码低于我如何使用阅读器。
protected bool TryLoadPlayerData(PlayerConnection playerConnection, string sso)
{
try
{
SetPlayer(playerConnection);
using (var databaseConnection = Hariak.HariakServer.Database.NewDatabaseConnection)
{
databaseConnection.SetQuery("SELECT * FROM `users` WHERE `auth_ticket` = @sso LIMIT 1;");
databaseConnection.AppendParameter("sso", sso);
using (MySqlDataReader reader = databaseConnection.ExecuteReader())
{
while (reader.Read())
{
// loop through columns here...
}
}
}
Logger.Info("Loaded PlayerData for " + SelectColumn("username"));
return true;
}
catch (Exception exception)
{
Logger.Error(exception, "Failed to load PlayerData for player.");
return false;
}
}
答案 0 :(得分:0)
我要做的就是遍历所有FieldCount,这样您就可以获得每个返回行的索引,然后使用GetName
和GetValue
protected bool TryLoadPlayerData(PlayerConnection playerConnection, string sso)
{
try
{
SetPlayer(playerConnection);
using (var databaseConnection = Hariak.HariakServer.Database.NewDatabaseConnection)
{
databaseConnection.SetQuery("SELECT * FROM `users` WHERE `auth_ticket` = @sso LIMIT 1;");
databaseConnection.AppendParameter("sso", sso);
using (MySqlDataReader reader = databaseConnection.ExecuteReader())
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
for (int i = 0; i < reader.FieldCount; i++)
{
dictionary.Add(reader.GetName(i), reader.GetValue(i));
}
}
}
Logger.Info("Loaded PlayerData for " + SelectColumn("username"));
return true;
}
catch (Exception exception)
{
Logger.Error(exception, "Failed to load PlayerData for player.");
return false;
}
}