tags:

views:

62

answers:

2

I am trying to retrieve some data from a table(Access 2007) and display this data in a textbox in a windows Form in C#,also I want to update with a button, but I cant get to work, this is my code for retrieve the data but I cannot display in a textbox, can someone help me?

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Policias.accdb");
        OleDbCommand command = new OleDbCommand();
        command.Connection = con;
        command.CommandText = "SELECT contenido FROM seccion";
        command.CommandType = CommandType.Text;
        con.Open();
        OleDbDataReader dr = command.ExecuteReader();


        while (dr.Read())
        {
            textBox3.Text = dr["cosa"].ToString();

        }

        dr.Close();
        con.Close();
+3  A: 

Below, I'll walk you through how to read data from an Access (.accdb) file, using the the OleDbDataReader, and store the information in a TextBox control. Before we dive into the code, there are a few things you need to make sure you're doing:

  1. Make sure you don't have the database open in Access (this can cause file access issues)
  2. Make sure you have copy of the database in your debug and release folders (located in the bin folder of your project folder.

Now for the code! I'll walk you through the process, step-by-step:

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourDatabase.accdb");

This line of code tells your program how to access your database. The Provider tells your program the kind of database you'll be connecting to, while the Data Source tells your program where the database is located.

OleDbCommand command = connection.CreateCommand();

This line of code creates a command object, associated with the database your program will be connected to.

command.CommandText = "SELECT ColumnWithTypeText FROM TableName";

This line of code is used to store the database command we want to use later. Because all we're interested in is the text stored in TableName's ColumnWithTypeText, we were explicit. If we wanted every item, from every column stored in TableName, we would use the following line instead:

command.CommandText = "SELECT * FROM TableName";

For more information on SQL commands, I recommend reading through this article. Not all the commands work for Access databases, but most will.

connection.Open();

This line of code opens the connection to the database.

OleDbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);

This line of code creates an OleDbDataReader that will use the CommandText we setup earlier. Because we know we'll be displaying this information in a textbox, we can use CommandBehavior.SingleResult to return one set of information at a time.

if (reader.HasRows)
{
   if (reader.Read())
   {
      textBox1.Text = reader["ColumnWithTypeText"].ToString();
   }
}
else
{
   textBox1.Text = "No rows found!";
}

This section of code checks to make sure that the DataReader has received information from the database, then tells it to read the information to us, displaying it in the TextBox control. If there is no information returned back to the reader, then "No rows found!" is displayed in the TextBox control. Because reader.Read() is in an if statement, it will only read once, starting with the first set of data. Other situations may call for a while loop instead.

reader.Close();
connection.Close();

Finally, you close your reader and your connection to the database.

All together, the code looks like this:

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourDatabase.accdb");
OleDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT ColumnWithTypeText FROM TableName";

connection.Open();

OleDbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);

if (reader.HasRows)
{
    if (reader.Read())
    {
        textBox1.Text = reader["ColumnWithTypeText"].ToString();
        //textBox1.Text = reader.GetString(0);
    }
}
else
{
    textBox1.Text = "No rows found!";
}

reader.Close();
connection.Close();

Hope this helps you (and others) out! :)

Ari Patrick
A: 

Thanks for your time in answering :D

Bye

Natasha