Initialize a list from a database table without data binding in C#

(Note that this is a very manual way to do this. Often you can display data more easily by using data binding.)

In this example, I added the Access database file Books.mdb to the project and set its "Copy to Output Directory" property to "Copy if Newer." The program uses OLE DB data objects so I added the following using statement to the code.

using System.Data.OleDb;

The following code shows how the program initializes a ListBox when it starts.

// The database connection.
private OleDbConnection Conn;

// Display a list of titles.
private void Form1_Load(object sender, EventArgs e)
{
    // Compose the database file name.
    // This assumes it's in the executable's directory.
    string db_name = Application.StartupPath + "\\Books.mdb"; 

    // Connect to the database
    Conn = new OleDbConnection(
        "Provider=Microsoft.ACE.OLEDB.12.0;" +
        "Data Source=" + db_name + ";" +
        "Mode=Share Deny None");

    // Get the titles.
    OleDbCommand cmd = new OleDbCommand(
        "SELECT Title FROM Books ORDER BY Title",
        Conn);
    Conn.Open();
    OleDbDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        lstTitles.Items.Add(reader.GetValue(0));
    }
    reader.Close();
    Conn.Close();
}

This code starts by declaring an OleDbConnection object named Conn. (This example could declare the object within the form's Load event handler but I'm making it class-level to make it easier to use in later examples.)

The form's Load event handler builds a path to the database file and uses it to create the OleDbConnection object.

Next the code creates an OleDbCommand that selects the Books table's Title field, ordering the result by Title. The program then performs a common database programming sequence: open the connection, execute the command, process the results, close the connection.

The program assigns the result of the command object's ExecutceReader method to an OleDbDataReader object.

To process the results, the program enters a loop that executes as long as the reader's Read method returns true. That method returns true if the reader is at a result record. The while loop begins by calling this method to move to the first record in the result set. After the loop has processed all of the returned records, the Read method returns false and the loop exits.

When it fetches a record, the loop adds the value of the first field (the Title field) to the ListBox.

This may all seem a bit cumbersome but it gives you total control over what's happening and once you get the hang of it (and possibly build this code into a helper method) it's fairly easy to use.

The next couple of examples show how to fetch other data without data binding.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.