Connecting to an Access Database with C#

Start with a new C# windows application in Visual Studio. Call it “AccessDBTutorial”. Download the sample Access database I have created for you from this URL: This database “people.mdb” consists of exactly one table called “students”, which looks like this:
username aalkasim aashok akalafut amutsudd anmcleve bkopecky cshue dwolfe gmawalan gweldon jebonner jiadeng kbrunett kmakice mbuddie mpgandhi prohwer rkale rvarick san shahak sskamath tjtucker ttoscos vvasudev Firstname Alia Arvind Andrew Adity Anne Brian Craig Daniel Gauri Matt Josh Jiahu Kynthia Kevin Michele Mona Paul Rupali Ryan Shunying Ankur Shreyas Tim Tammy Varun lastname Al Kasimi Ashok Kalafut Upoma Faber Kopecky Shue Wolfe Mawalankar Weldon Bonner Deng Brunette Makice Buddie Gandhi Rohwer Kale Varick An Shah Kamath Tucker Toscos Vasudev department CS HCI CS CS CS CS CS MIME CS HCI CS CS HCI HCI MIME CS CS CS HCI HCI CS CS HCI HCI CS email project_group 7 3 7 3 8 2 5 2 6 5 5 6 2 7 3 8 6 2 6 8 3 6 5 8 7

We are going to create a very simple C# application that issues an SQL query to this database and returns the result. It should look very similar to this ☺, Thanks Tammy for being the example.

Lets get started…

From the list right-click Data Connections and choose 'Add Connection.0 OLE DB Provider (used for connecting to an Access database).. Leave the defaults and just press OK.Admin'. The 'Data Adapter Configuration Wizard' starts. and click Next. test the connection by clicking the 'Test Connection' button.X:\PathToYourDatabase\people. As I said.The first thing we need to do is make a connection to the Access database (people.. Use the '.mdb). you can browse the Access file database.mdb. Press Next and then again Next (leave the default 'Use SQL statements'). It should say 'Test connection succeeded. Clicking next will take you to the part where you need to select the connection you wish to use. The node is named using the form 'ACCESS.mdb. After clicking OK.’ Press OK and a window pops up that says 'Please Enter MS JET OLE DB Initialization Information'.mdb.'.. Just like you see in the above screenshot. .. Choose the connection we have just created (ends up in people. In the 'Provider' tab select Microsoft Jet 4.Admin). open the Server Explorer by clicking (Ctrl+Alt+S). For this you need to drag the node below 'Data Connections' on the form. people. drag it on the form and 'oleDbConnection1' should appear below the form: Next open the Toolbox (Ctrl+Alt+X) and from the 'Data' group drag an 'OleDbDataAdapter'. Yet the database is not connected to our program.' button to browse for an Access Database and choose the database you have downloaded.

First we should set the string we’ll use for our SQL query: String sqlQuery = "SELECT firstname. Choose 'Untyped DataSet (no schema)'. Now create a form that looks like this: label1 label2 button1 textfield1 Now doubleclick button1 so we can put code into the button1_Click event. Add it and close the small window and now we have a small window representing the table.ToString()+" \'".. The following SQL query is created: SELECT students. DataSets are used to store the query results..* FROM students We could also do it more simple by typing 'SELECT * FROM students' FROM students where username = \'"+textBox1.Text. Open the Query Builder using the button and you should now be able to add the table named 'students'. Now add a DataSet to our application by dragging one from the Data group (Toolbox). therefore check '* (All Columns)'. . it would have been the same. We want to select all the columns. We want all the tables and all the columns therefore we need to take the following steps. Press OK to exit the Query Builder and then press Finish.Now you are being asked 'What data should the data adapter load in the dataset?'.

numberOfRowsFeched = oleDbDataAdapter1.SelectCommand. System.Rows[0][1]. to get at the data inside there we have to use an object called DataTable."students").Fill(dataSet1. Next we use the 'Fill()' method of oleDbDataAdapter1 to fill the dataSet1 with the result of the SQL query: int numberOfRowsFeched = oleDbDataAdapter1.Clear().lastname.ToString(). label1.EventArgs e) { sqlQuery = "SELECT firstname.Text = dt.ToString() + " " + dt.ToString(). label2. notice that we captured it as numberOfRowsFeched. Next we clear the DataSet preparing it for the operation dataSet1. add these lines below what you have already… if (numberOfRowsFeched > 0) { DataTable dt = dataSet1. So. "students").Rows[0][2].Rows[0][0]. } else { label1.ToString().Text = dt. label1.CommandText = sqlQuery.Text = dt. set the SQL query in our oleDbDataAdaptor… oleDbDataAdapter1.ToString(). if (numberOfRowsFeched > 0) { DataTable dt = dataSet1.CommandText = sqlQuery.Rows[0][0].Text = "Name not found.Text = "".Text = "".Clear(). label2. } Here is all the code we wrote in one place: private void button1_Click(object sender. label1. FROM students where username = \'"+textBox1.ToString() + " " + = "Name not found.Rows[0][2].Text. run your program to see if it works!!! .SelectCommand. dataSet1.Rows[0][1].Tables["students"].". } } Now. label1. the Fill() method returns the number of rows that were fetched when the query was run.Fill(dataSet1.". At this point we have a DataSet with the result of our query in it.Text = dt. } else { label1.ToString()+" \'".Tables["students"].

and find out the ID of 5 RFID tags. you’ll need to make a way that it only calls the SQL query once. and make it so that when you detect an RFID object the application queries that record from the database. Hints: When the RFID sensor detects an RFID object it calls the rfid1_Tag function repeatedly. Once you have the records in the database tagged.Console.Writeline so you can see them.Your assignment: Add a PhidgetRFID sensor to this project. associate those tags with some of the records in the people.mdb database. Spit the values from your RFID tags to System. My final Form looks like this after I touch one of my RFID tags to the RFID reader… 010238ed56 . and copy and paste them into some text editor so you can add them to the database. add some code to the application you created above.

Sign up to vote on this title
UsefulNot useful