create a microsoft access database

5
Create a Microsoft Access database To create a database in Access and then create a table in the database, follow these steps: 1. Start Access. 2. On the File menu, click New. 3. Click Blank database on the task pane. 4. In the File name box, type Dan23b, and then click Create. 5. Right-click Create table in Design view, and then click Open. 6. In the Field Name text box, type StudentName. 7. In the Data Type list, click Text. 8. In the Field Name text box, type IDNo. 9. In the Data Type list, click Number. 10.Right-click IDNo, and then click Primary Key. 11.On the File menu, click Save As. 12.In the Save As dialog box, type Dan23 in the text box and then click OK. Close the design view. 13.Right-click the Student table, and then click Open. 14.Type ABC in the StudentName column. 15.Type 101 in the IDNo column. 16.Type XYZ in the StudentName column. 17.Type 102 in the IDNo column. 18.Type several more records in the Student table, and then close the Student:Table window. 19.Close the Dan23 database. Create a connection to the Access database by using Visual Basic .NET The 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 1. Start Microsoft Visual Studio .NET. 2. On the File menu, point to New, and then click Project.

Upload: shamesimi

Post on 27-Dec-2015

14 views

Category:

Documents


2 download

DESCRIPTION

Create a Microsoft Access Database

TRANSCRIPT

Page 1: Create a Microsoft Access Database

Create a Microsoft Access database

To create a database in Access and then create a table in the database, follow these steps:

1. Start Access.2. On the File menu, click New.3. Click Blank database on the task pane.4. In the File name box, type Dan23b, and then click Create.5. Right-click Create table in Design view, and then click Open.6. In the Field Name text box, type StudentName.7. In the Data Type list, click Text.8. In the Field Name text box, type IDNo.9. In the Data Type list, click Number.10.Right-click IDNo, and then click Primary Key.11.On the File menu, click Save As.12.In the Save As dialog box, type Dan23 in the text box and then click OK.

Close the design view.13.Right-click the Student table, and then click Open.14.Type ABC in the StudentName column.15.Type 101 in the IDNo column.16.Type XYZ in the StudentName column.17.Type 102 in the IDNo column.18.Type several more records in the Student table, and then close the

Student:Table window.19.Close the Dan23 database.

Create a connection to the Access database by using Visual Basic .NET

The 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 .NET1. Start Microsoft Visual Studio .NET.2. On the File menu, point to New, and then click Project.3. Under Project Types, click Visual Basic Projects.4. Under Templates, click Windows Application, and then click OK. 

By default, Form1 is created.Open a connection to the Access database

1. On the View menu, click Server Explorer.2. In Server Explorer, right-click Data Connections, and then click Add

Connection.

Page 2: Create a Microsoft Access Database

3. In the Data Link Properties dialog box, click the Provider tab.4. In the OLE DB Provider(s) list, click Microsoft Jet 4.0 OLE DB Provider,

and then click Next.5. Click the Connection tab, and then click the ellipses button (...).6. Locate the Access database testdb.mdb file that you created by following the

corresponding path on your computer.7. Select the testdb.mdb file, and then click Open.8. In the Data Link Properties dialog box, click OK.

Retrieve data from the Access database by using the OleDbDataAdapter class

1. On the toolbox, click the Data tab.2. Drag an OleDbDataAdapter control to Form1.3. In the Data Adapter Configuration Wizard, click Next three times.4. In the Generate the SQL statements panel, type the following Microsoft

SQL Server statement, and then click Next:Select * from Student

5. 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.

6. Right-click OleDbDataAdapter1, and then click Generate Dataset.7. In the Generate Dataset dialog box, click OK.8. Add the following code to the Form1_Load event handler:9. 'Fill retrieves rows from the data source by using the SELECT statement

OleDbDataAdapter1.Fill(DataSet11)

Display records that are retrieved from the Access database

1. Add a DataGrid control to Form1. 

By default, DataGrid1 is created.2. Right-click DataGrid1, and then click Properties.3. In the Properties dialog box, set the DataSource property

to DataSet11 and set the DataMember property to Student.

Add a row to a table in the Access database

1. Add a Button control to Form1.2. Right-click Button1, and then click Properties.3. In the Properties dialog box, set the Text property to Add.4. Add the following code to the Button1_Click event handler:5. Dim i, sno As Integer6. Dim sname As String7. Dim rw As DataRow8. 'Add a new row to the Student table.9. rw = DataSet11.Tables(0).NewRow10. sno = InputBox("Enter the Roll no of the Student:")11. sname = InputBox("Enter the Name of the Student:")

Page 3: Create a Microsoft Access Database

12. rw.Item("SNo") = sno13. rw.Item("SName") = sname14. Try15. DataSet11.Tables(0).Rows.Add(rw)16. 'Update the Student table in the testdb database.17. i = OleDbDataAdapter1.Update(DataSet11)18. Catch ex As Exception19. MessageBox.Show(ex.Message)20. End Try21. 'Displays number of rows updated.

MessageBox.Show("no of rows updated=" & i)

Update the rows of a table in the Access database

1. Add a Button control to Form1. 

By default, Button2 is created.2. Right-click Button2, and then click Properties.3. In the Properties dialog box, set the Text property to Update.4. Add the following code to the Button2_Click event handler:5. Dim i, rwno As Integer6. Dim colname As String7. Dim value As Object8. colname = InputBox("Enter the name of the Column to be updated")9. rwno = InputBox("Enter the Row Number to be updated: Row No starts from

0")10. value = InputBox("Enter the value to be entered into the Student

table")11. Try12. 'Update the column in the Student table.13. DataSet11.Tables(0).Rows(rwno).Item(colname) = value14. 'Update the Student table in the testdb database.15. i = OleDbDataAdapter1.Update(DataSet11)16. Catch ex As Exception17. MessageBox.Show(ex.Message)18. End Try19. 'Displays number of rows updated.20. MessageBox.Show("no of rows updated=" & i)

Delete rows from a table in the Access database

1. Add a Button control to Form1. 

By default, Button3 is created.2. Right-click Button3, and then click Properties.3. In the Properties dialog box, set the Text property to Delete.4. Add the following code to the Button3_Click event handler:5. Dim i As Integer6. Dim rno As Integer7. rno = InputBox("Enter the Row no to be deleted: Row no starts from 0")8. Try9. 'Delete a row from the Student table.

Page 4: Create a Microsoft Access Database

10. DataSet11.Tables(0).Rows(rno).Delete()11. 'Update the Student table in the testdb database.12. i = OleDbDataAdapter1.Update(DataSet11)13. Catch ex As Exception14. MsgBox(ex.Message)15. End Try16. 'Displays number of rows updated.

MessageBox.Show("no of rows updated=" & i)

Verify that it works

1. On the Build menu, click Build Solution.2. On the Debug menu, click Start.3. 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.4. Click Update, and then type the data in the input boxes to update a column

in the Student table.5. Click Delete, and then type the data in the corresponding input boxes to

delete a row from the Student table.