データベースからデータを取得し、List<> に入力しようとしています。それが私の目標です。
現在、取得したデータをメソッド内の変数に挿入し、そのメソッドを List<> に追加しようとしています。
これが私のコードです:
public void Select()
{
var sprite = new Sprite();
if (Game.player.inBattle)
{
//Open a connection
databaseCon.Open(); //Works
//Create Command
var cmd = new MySqlCommand(BattleEngine.query, databaseCon); //Works
//Create a data reader and Execute the command
var dataReader = cmd.ExecuteReader(); //Works
try
{
//Read the data and store them in the list
while (dataReader.Read())
{
//Inserts matching Row's first Column attribute into method variable
sprite.id = (dataReader.GetInt32(0));
//Inserts matching Row's second Column attribute into method variable
sprite.identifier = (dataReader.GetString(1));
Console.WriteLine(sprite.id + ": " + sprite.identifier); //Works. Variables are loaded with data
//Fails. Doesn't load the method into the list
Game.sprite.Add(new Sprite());
}
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex);
}
finally
{
if (dataReader != null)
{
//close Data Reader
dataReader.Close();
}
if (databaseCon != null)
{
//close Connection
databaseCon.Close();
}
}
}
}
Console.WriteLine(Game.sprite[0]);
このループの後にテスト設定があり、表示されますNamespace.Sprite
これを行うにはおそらく他の方法がありますが、最終的には、新しいメソッド内の各レコードをリストに入力したいと考えています。それを List<> に格納することで、後で使用する特定の情報を取得できます。
理論的には、これができるようになりたいと思っています。
Console.WriteLine("{0}", Game.sprite[0].id);
以前にこれが行われたのを見たことがありますが、再現できないようです。
添加:
public class Game
{
public static Player player;
public static List<Character> sprite;
GameScreens mainMenu = new GameScreens();
GameScreens titleScreen = new GameScreens();
public Game()
{
Console.SetWindowSize(100, 50);
Console.BufferHeight = 50;
Console.BufferWidth = 100;
titleScreen.TitleScreen();
player = new Player();
sprite = new List<Character>();
Player.Initialize(player);
GameLoop();
}
void GameLoop() //Our game loop.
{
mainMenu.MainMenu();
}
}