How to use Microsoft Visual Basic .NET to connect to a Microsoft Access database and to retrieve data (821765)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SUMMARYThis step-by-step article describes how to use Microsoft
ADO.NET to open a Microsoft Access database by using the OLE DB data provider.
You use the OleDbDataAdapter class to obtain rows from the Access database and to insert the
rows into a DataSet object. This article describes how to add rows, how to delete
rows, and how to update rows in the Access database by using DataSet and OleDbDataAdapter. back to the
topRequirementsThe following list outlines the recommended hardware,
software, network infrastructure, and service packs that are required:
- Microsoft .NET Framework
- Microsoft Visual Basic .NET
- Microsoft Access
This article assumes that you are familiar with the following
topics:
- Visual Basic .NET
- ADO.NET
- Access
back to the top Create a Microsoft Access databaseTo create a database in Access and then create a table in the
database, follow these steps:
- Start Access.
- On the File menu, click
New.
- Click Blank database on the task
pane.
- In the File name box, type
testdb, and then click
Create.
- Right-click Create table in Design view,
and then click Open.
- In the Field Name text box, type SName.
- In the Data Type list, click
Text.
- In the Field Name text box, type SNo.
- In the Data Type list, click
Number.
- Right-click SNo, and then click
Primary Key.
- On the File menu, click Save
As.
- In the Save As dialog box, type
Student in the
text box
and then click OK. Close the design view.
- Right-click the Student table, and then click
Open.
- Type ABC in the
SName column.
- Type 101 in the
SNo column.
- Type XYZ in the
SName column.
- Type 102 in the
SNo column.
- Type several more records in the Student
table, and then close the Student:Table window.
- Close the testdb database.
back to the topCreate a connection to the Access database by using Visual Basic .NETThe following step-by-step example describes how to create a
connection to the Access database by using the Microsoft Visual Studio .NET Server Explorer. The following example also describes how to use the OleDbDataAdapter class to retrieve data from the database and to insert data into
a DataSet object. This example also describes how to create new rows, how
to add these rows to the table, how to modify the data in the rows, and how to
remove rows from the table in the Access database. Create a Windows application in
Visual Basic .NET- Start Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project
Types, click Visual Basic Projects.
- Under Templates, click Windows
Application, and then click OK.
By default,
Form1 is created. Open a connection to the Access database- On the View menu, click Server
Explorer.
- In Server Explorer, right-click Data
Connections, and then click Add
Connection.
- In the Data Link Properties dialog box,
click the Provider tab.
- In the OLE DB Provider(s) list, click
Microsoft Jet 4.0 OLE DB Provider, and then click
Next.
- Click the Connection tab, and then click
the ellipses button (...).
- Locate the Access database testdb.mdb file that you created
by following the corresponding path on your computer.
- Select the testdb.mdb file, and then click
Open.
- In the Data Link Properties dialog box,
click OK.
Retrieve data from the Access
database by using the OleDbDataAdapter class- On the toolbox, click the Data
tab.
- Drag an OleDbDataAdapter control to
Form1.
- In the Data Adapter Configuration Wizard,
click Next three times.
- In the Generate the SQL statements panel,
type the following Microsoft SQL Server statement,
and then click Next:
Select * from Student - In the View Wizard Results panel, click
Finish.
Note In the Do you want to include the password in the
connection string? dialog box, click Don't include
password. - Right-click OleDbDataAdapter1, and then
click Generate Dataset.
- In the Generate Dataset dialog box, click
OK.
- Add the following code to the Form1_Load
event handler:
'Fill retrieves rows from the data source by using the SELECT statement
OleDbDataAdapter1.Fill(DataSet11) back to the topDisplay records that are retrieved from the Access database- Add a DataGrid control to Form1.
By default, DataGrid1 is created. - Right-click DataGrid1, and then click
Properties.
- In the Properties dialog box, set the
DataSource property to DataSet11 and set the
DataMember property to Student.
back to the topAdd a row to a table in the Access database- Add a Button control to Form1.
- Right-click Button1, and then click
Properties.
- In the Properties dialog box, set the
Text property to Add.
- Add the following code to the
Button1_Click event handler:
Dim i, sno As Integer
Dim sname As String
Dim rw As DataRow
'Add a new row to the Student table.
rw = DataSet11.Tables(0).NewRow
sno = InputBox("Enter the Roll no of the Student:")
sname = InputBox("Enter the Name of the Student:")
rw.Item("SNo") = sno
rw.Item("SName") = sname
Try
DataSet11.Tables(0).Rows.Add(rw)
'Update the Student table in the testdb database.
i = OleDbDataAdapter1.Update(DataSet11)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Displays number of rows updated.
MessageBox.Show("no of rows updated=" & i) back to the topUpdate the rows of a table in the Access database- Add a Button control to Form1.
By default, Button2 is created. - Right-click Button2, and then click
Properties.
- In the Properties dialog box, set the
Text property to Update.
- Add the following code to the
Button2_Click event handler:
Dim i, rwno As Integer
Dim colname As String
Dim value As Object
colname = InputBox("Enter the name of the Column to be updated")
rwno = InputBox("Enter the Row Number to be updated: Row No starts from 0")
value = InputBox("Enter the value to be entered into the Student table")
Try
'Update the column in the Student table.
DataSet11.Tables(0).Rows(rwno).Item(colname) = value
'Update the Student table in the testdb database.
i = OleDbDataAdapter1.Update(DataSet11)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Displays number of rows updated.
MessageBox.Show("no of rows updated=" & i)
back to the topDelete rows from a table in the Access database- Add a Button control to Form1.
By default, Button3 is created. - Right-click Button3, and then click
Properties.
- In the Properties dialog box, set the
Text property to Delete.
- Add the following code to the
Button3_Click event handler:
Dim i As Integer
Dim rno As Integer
rno = InputBox("Enter the Row no to be deleted: Row no starts from 0")
Try
'Delete a row from the Student table.
DataSet11.Tables(0).Rows(rno).Delete()
'Update the Student table in the testdb database.
i = OleDbDataAdapter1.Update(DataSet11)
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Displays number of rows updated.
MessageBox.Show("no of rows updated=" & i) back to the topVerify that it
works- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Add, and then type the data in the
input
box to add a row to the
Student table.
Note: You receive an error if you click Cancel in the
input boxes. - Click Update, and then type the data in
the input boxes to update a column in the
Student table.
- Click Delete, and then type the data in
the corresponding input boxes
to delete a row from the Student
table.
back to the
topREFERENCESFor more information, visit the following Microsoft
Developer Network (MSDN) Web sites: back to the top
Modification Type: | Minor | Last Reviewed: | 9/13/2005 |
---|
Keywords: | kbtable kbHOWTOmaster kbhowto kbSystemData kbDatabase kbDataAdapter KB821765 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|