5 Replies - 1656 Views - Last Post: 25 February 2013 - 09:47 AM Rate Topic: -----

#1 Fish-E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-August 12

Outputting data from an .accdb database to a textbox?

Posted 24 February 2013 - 02:49 PM

Hi

Basically I'm looking for some help outputting data from a database to a text box in my wpf.

I've looked at a lot of things on the internet and I'm fairly certain I've managed to link the database to the program (Console.WriteLine(MyConn.State); outputs open) but the problem arises when I try and output the data. I simply have no idea how!

I've looked at a few examples on the internet, but none of them have been very helpful as they're either not applicable (ie output to a datagridview) or they aren't explained.

I'm also self-taught (and I'm hardly knowledgeable!) so if anyone is willing to help me I'd appreciate it if you took it slow and explained what you're doing!

Here is some code I've found on the internet, which I've adapted and then added a few comments to.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace connectionDatabase1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Adam\Documents\ConnectionDatabase.accdb;Jet OLEDB:Database Password=password"; //Selects the database along with the provider of the database
            string mySQL = "SELECT * FROM Test"; //SQL code required

            OleDbConnection MyConn = new OleDbConnection(ConnStr); 
            //starts new class called MyConn. This represents the connection to the database

            MyConn.Open(); //opens MyConn

            OleDbCommand Cmd = new OleDbCommand(mySQL, MyConn); 
            //starts new class called Cmd. This creates commands to be used on the database
            OleDbDataReader ObjReader = Cmd.ExecuteReader();
            //starts new class called ObjReader. This reads a row at a time.

            if (ObjReader != null) //While a row contains data
            {
                //Do this
            }

            Console.WriteLine(MyConn.State); //outputs the statues of the database, whether it's opened or not

            ObjReader.Close(); //stop reading
            MyConn.Close(); //close the connections
        }

    }
}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Outputting data from an .accdb database to a textbox?

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,602
  • Joined: 12-December 12

Re: Outputting data from an .accdb database to a textbox?

Posted 24 February 2013 - 03:11 PM

There is a simple example here at MSDN of reading data from a DataReader. Click the Get.. links on the left to obtain other ways to retrieve the field(s) data.

It depends:
How many fields are retrieved from the table? How many rows?
Do you want to retrieve the data from a specific column or field-name?
Your post states that you want to put the data into a single TextBox; is this correct?

This post has been edited by andrewsw: 24 February 2013 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,602
  • Joined: 12-December 12

Re: Outputting data from an .accdb database to a textbox?

Posted 24 February 2013 - 03:20 PM

In general it is better to use Data Binding - to link the form to a data-source.
Was This Post Helpful? 2
  • +
  • -

#4 Fish-E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-August 12

Re: Outputting data from an .accdb database to a textbox?

Posted 25 February 2013 - 08:42 AM

View Postandrewsw, on 24 February 2013 - 03:11 PM, said:

There is a simple example here at MSDN of reading data from a DataReader. Click the Get.. links on the left to obtain other ways to retrieve the field(s) data.

It depends:
How many fields are retrieved from the table? How many rows?
Do you want to retrieve the data from a specific column or field-name?
Your post states that you want to put the data into a single TextBox; is this correct?


I've had a look at the code and it's not explained, nor does it want to work. OracleCommand (etc) cannot be found and the article does not explain how to solve this. A quick google search indicates that I need to use using System.Data.OracleClient; however OracleClient does not exist in the namespace System.Data.

At the moment it's connected to a very simple database. The database has one table (Test) which has 2 fields with one record ("2 x 1"). The finish product will have multiple tables, each with multiple fields and multiple records. Each textbox in the program will contain the data from one field / records at a time.

I don't know if I've explained that very well. I can draw a diagram if it'll help.

View Postandrewsw, on 24 February 2013 - 03:20 PM, said:

In general it is better to use Data Binding - to link the form to a data-source.


I'll read through this.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,602
  • Joined: 12-December 12

Re: Outputting data from an .accdb database to a textbox?

Posted 25 February 2013 - 09:30 AM

That sample connects to an Oracle database whereas you are using an Access database. The actual connection details are insignificant and not central to the example code.

However, as you were unable to make sense of that example then I suggest you need to find some first-steps tutorials. There may be some here at DIC.
Was This Post Helpful? 0
  • +
  • -

#6 Fish-E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-August 12

Re: Outputting data from an .accdb database to a textbox?

Posted 25 February 2013 - 09:47 AM

Actually thanks to your second post I'm now able to output data from the database :)

I've now got to work out how to make the text box update when I press a button (to navigate to the next record)!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1