2

I am trying to populate a group of labels in a C# windows form with some values that are in a certain attribute (PlayerName) in a database that I have in access.

Currently the only code I have is:

OleDbConnection connection = new OleDbConnection(CONNECTION STRING HERE);

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT PlayerName FROM [TotalPlayerName] WHERE Team = 1 AND SportID = " + Form1.IDNumber;

I need a list or array that holds these values so I can use them to populate the labels, but I am unaware of how to do this.

Loofer
  • 6,841
  • 9
  • 61
  • 102
Alby
  • 27
  • 1
  • 8

5 Answers5

2

You need to call ExecuteReader to obtain a data reader and then loop through the rows of the result set like this:

List<string> result = new List<string>();

using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        result.Add(reader.GetString(0));
    }
}

Before you do this, don't forget to open the connection like this:

connection.Open();
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
1

There are a couple of things here..... for sake of best practice well its more standard practice... as I like to say!

Use USING as this cleans up after connection.. see here for great examples in a "using" block is a SqlConnection closed on return or exception?

using (OdbcDataReader DbReader = DbCommand.ExecuteReader())
    {
       int fCount = DbReader.FieldCount;
       while (DbReader.Read())
         {
              Label1 = DbReader.GetString(0);
              Label2 = DbReader.GetString(1);
              Label3 = DbReader.GetString(2);
              Label4 = DbReader.GetString(3);

              for (int i = 0; i < fCount; i++)
                {
                   String col = DbReader.GetString(i);
                    Console.Write(col + ":");
                }
                Console.WriteLine();
        }
}

NB your SQL only return 1 field /String at the moment

Community
  • 1
  • 1
Ggalla1779
  • 476
  • 7
  • 18
0

One option might be using OleDbDataAdapter to fill a DataTable those values that returns your query;

var dt = new DataTable();
using(var da = new OleDbDataAdapter(command))
{
    da.Fill(dt);
}

And since your query return one column, you can use AsEnumerable to that datatable to get them as a string like;

List<string> list = dt.AsEnumerable()
                      .Select(r => r.Field<string>("PlayerName"))
                      .ToList();

You can read: Queries in LINQ to DataSet

By the way, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

Also use using statement to dispose your connection and command automatically as I did for OleDbDataAdapter in my example.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

while reading the data fill the list like

List<string> players = new List<string>();

OleDbDataReader rdr = command.ExecuteReader();
While(rdr.Read())
{
  players.Add(rdr["PlayerName"].ToString());
}
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

You need to create a OleDbReader object to read the response from the query. You will also need to create a List to store the data Something like this:

List<string> playerNameList = new List<string>(); 
using (OleDbReader r = command.ExecuteReader())
{
   while(reader.Read())
   {
     playerNameList.Add(reader.GetString(0));
   }
}
WylieG
  • 76
  • 3