C# program that interface with MS Access: Read data (text, image, etc) from an Access database file

1.      Open a new Visual C# .NET windows application. Name the project “Store Data”.

2.      Design a form. Add two Textboxes, a Button and a Picturebox

3.      Set Name property of Textboxes and PictureBox to “txtDatabase”, “txtStudentName” and “picStudent”.

4.      Set Name property of Button to “btnReadData” with text property “Read Data”.

5.      In the code file add namespace:

using System.Text;

using System.Data.OleDb;

6.      Double click on the Button “Read Data” and pest the following code into the “btnReadData_Click” event.

string sql = "Select * from
tblStudentInfo";
string ConnectionString ="Provider=Microsoft.Jet.OleDb.4.0;data
source=" + txtDatabase.Text + "";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, ConnectionString);
            DataTable dtStudent = new
DataTable();
           
adapter.Fill(dtStudent);
 
txtStudentName.Text =
dtStudent.Rows[0]["StudentName"].ToString();
 
            byte[] content = (byte[])dtStudent.Rows[0]["Picture"];
            try
            {
                MemoryStream stream = new
MemoryStream(content);
               
picStudent.Image = Image.FromStream(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                MessageBox.Show(ex.StackTrace.ToString());
            }

7.      Press F5 to build and run the project.

8.      Put your database name with location like c:\MyAccessDB in the text box “txtDatabase” and then press the “Read Data” Button. Student Name and Student Picture will be shown in the form.

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working