C# program that interface with MS Access: Conduct a simple SEARCH operation on an Access database file

1.      Open a new Visual C# .NET windows application. Name the project “Search 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 “btnSearch” with text property “Search Data”.

5.      In the code file add namespace:

using System.Text;

using System.Data.OleDb;

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

string sql = "Select * from
tblStudentInfo where StudentName='" + txtStudentName.Text + "'";
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);
 
            if (dtStudent.Rows.Count > 0)
            {
   
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());
                }
            }
            else
            {
                txtStudentName.Text = "";
               
picStudent.Image = null;
                MessageBox.Show("Student
not found");
            }

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 put student name in the “Student Name” text box and then press “Search Data” Button. If putted student name found the database the 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