Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control. Bind and display data from databases to textbox and label controls. Write procedures to navigate table rows in a dataset. Display a row number on a form. Create a parameterized query. Write procedures to perform data maintenance activities including Add, Edit, and Delete rows. Use data validation routines in a data maintenance program.
Database Files What is a data base? Use the internet to give me an answer
Answer: database – a special repository— sometimes a single file, sometimes multiple files—used to store and to retrieve information.
Relational database –What is a relational database? You find the answer for me:
Answer a specific type of database where data rows are stored in separate tables, and the tables are related to each other by key values
What we will use We will use Microsoft Access Perfect for small-sized, individual user or small group systems. We will look at how vb.net and access can work together.
How do we connect the two? VB.NET uses ADO.NET – a database technology that supports connection to different database products. ADO.NET stores and transfers data using the Extensible Markup Language (XML). ADO.NET uses two basic types of connections to databases across networks: SQLClient – used to connect to Microsoft's SQL Server DBMS. We won’t use this one OLEDB – used to connect to all other database formats. We will use this one.
Database Concepts and Terminology A database stores data about individual entities in separate tables. Examples of entities in a school setting include Students, Courses, and Enrollment in courses.
Table Explained Each table consists of rows and columns. Row = row for an individual student; also referred to as a record. Column = field of data stored for each student such as the SSN, Last Name, First Name, etc.
ADO.net provides controls that you add to a form that are used to connect to and manage data in a database table. Columns from a database table are bound to database controls. Some of the controls you've already used such as labels, textboxes, and combo boxes can be found to database columns. Other special data controls such as the DataGrid and DataList controls need to be looked at.
Connecting VB and a database Configure a connection. The connection links to a data source – a data source is either a specific database file and/or a server computer. Configure a data adapter. A data adapter handles data retrieval and updating. The data adapter creates a dataset. A dataset stores rows that have been retrieved. Add controls to the form and set properties of the controls to bind the controls to the columns of a specific table in the dataset. Write VB code to fill the dataset.
Storing your database You should always store your database file on drive and folder: C:\Temp – this needs to be setup on your computer. Why… because of network security issues you will need to copy the database each day to the temp folder.
Creating a Connetion We will use OleDbConnection
Class Exercise Copy the VBUniv.Mdb Microsoft Access database from the class server to your client computer to drive and folder: C:\Temp
Starting the project Download the VBUniv.Mdb Microsoft Access database to the c:\temp Begin a new project – name it Database Name the form frmStudent. Copy the VBUniv.MDB database file from C:\Temp to your project folder. This will be a copy for use in the event that the file on C:\Temp becomes corrupted.
Configuring a Data Adapter and Connection Add a data adapter by using the Data Adapter Configuration Wizard – add the OleDbDataAdapter control from the toolbox Data tab by double-clicking
The Connection tab Locate the VBUniv.MDB database file at C:\Temp. Leave the User name as Admin with no password—a password is not needed for Microsoft Access as it is basically a single- user database management system. Click the Test Connection button. The connection should succeed if it doesn't, use the Browse button again to ensure you have the correct database or use the Provider tab to ensure you have the correct OLE DB Provider.
Click the Query Builder button. All of the tables in the VBUniversity database (States, Course, Enrollment, and Student) are listed. If the Add Table window is not shown, right- click on the Query Builder window to open the Add Table window. Select the Student table and close the Add Table window.
Why use the Query Builder The Query Builder window interface is used to select the columns from the STUDENT table to include in the SQL Select statement. The columns (fields) are selected in the order in which they are checked. Notice that the output will be sorted by student's LastName column. Select ONLY the StudentSSN, LastName, FirstName, and MiddleInitial columns. You can specify the data rows to be sorted by last name.
Finishing up Examine your VB project form. You will discover the addition of two controls in the component tray named OleDbDataAdapter1 and OleDbConnection1. Rename the OleDbConnection1 control conVBUniversity.
Defining a Dataset Select the OleDbAdapter named daStudent, Select Generate Dataset from the Data menu, or Right-click and select from the shortcut menu. Name a dataset with the prefix "ds"—here we will name the dataset dsStudent.
After you click OK, you'll see the dataset listed in the system tray with the name DsStudent1 – Visual Basic automatically adds the digit 1 to the first dataset, 2 to the second dataset, and so forth.
The Fill Method At runtime, the data adapter's Fill method is executed to fill the dataset. This is usually coded in the Form_Load event for Windows applications. Examples: ‘DataAdapterName.Fill(DataSetName) daStudent.Fill(DaStudent1) When there is more than one table in a dataset, you can specify the dataset name and table name. daStudent.Fill(DsStudent1, "Student")
Binding Data to Controls A dataset is bound to controls on a form by setting control properties. When binding to a DataGrid, these properties include the: DataSource property – set it to the name of the dataset. DataMember property – set it to the name of the table.