ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

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

Updated on October 13, 2009

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
            if (dtStudent.Rows.Count > 0)
txtStudentName.Text = dtStudent.Rows[0]["StudentName"].ToString();
                byte[] content = (byte[])dtStudent.Rows[0]["Picture"];
                    MemoryStream stream = new
                    picStudent.Image = Image.FromStream(stream);
                catch (Exception ex)
                txtStudentName.Text = "";
picStudent.Image = null;
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.


    0 of 8192 characters used
    Post Comment

    No comments yet.