Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS16 Application Programming with Visual Basic

Similar presentations


Presentation on theme: "CIS16 Application Programming with Visual Basic"— Presentation transcript:

1 CIS16 Application Programming with Visual Basic
Chapter twelve Database queries

2 Creating a Query You use a database Query to retrieve specific information from a database specify both the records to select in a dataset and the order in which to arrange the records Can filter records to limit the records you want to view Can sequence records to arrange (sort) the records in either ascending (the default) or descending order by one or more fields Can use Aggregate operators to perform arithmetic calculations on groups of fields An aggregate operator returns a single value from a group of values

3 Select Statement The select statement is the most commonly used statement to create database queries in Structured Query Language, or SQL It is used to query the database and retrieve selected data that match the criteria that you specify SYNTAX: select "column1“ [,"column2",etc] from "tablename" [where "condition"] [order by column1 [asc|desc]; NOTE: [] designates an optional parameter The column names that follow the select keyword determine which columns will be returned in the results. You can select as many column names that you'd like, or you can use a "*" to select all columns.

4 Select Statement The table name that follows the keyword from specifies the table that will be queried to retrieve the desired results. The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where. Conditional selections used in the where clause: = Equal > Greater than < Less than >= Greater than or equal <= Less than or equal <> Not equal to LIKE *

5

6 Select Statement Like is a very powerful operator that allows you to select only rows that are "like" what you specify The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. SELECT first, last, city FROM empinfo WHERE first LIKE 'Er%'; This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. SELECT first, last FROM empinfo WHERE last LIKE '%s'; This statement will match any last names that end in a 's'. SELECT * FROM empinfo WHERE first = 'Eric'; This will only select rows where the first name equals 'Eric' exactly.

7 Select Statement 1.Display the first name and age for everyone that's in the table. 2.Display the first name, last name, and city for everyone that's not from Payson. 3.Display all columns for everyone that is over 40 years old. 4.Display the first and last names for everyone whose last name ends in an "ay". 5.Display all columns for everyone whose first name equals "Mary". 6.Display all columns for everyone whose first name contains "Mary".

8 Inserting data into a table
Adds new rows to the table

9 Updating data in a table
Updates fields

10 Deleting data in a table
Removes records

11 Creating queries

12 Creating a Query Right-click the DataSet.xsd file in the Solution Explorer window Contains information about the tables, fields, records, and properties Click Open to open the DataSet Designer window.

13 Creating a Query Right-click TableAdapter in the DataSet Designer window, choose Add (or AddQuery) and then click Query. The TableAdapter Query Configuration Wizard starts The Use SQL statements radio button is selected by default

14 Creating a Query The Choose a Query Type screen presents options for the different types of queries

15 Creating a Query The default query is automatically presented and also executed when the Load procedure invokes the TableAdapter object’s Fill method. This can be customized in the “What data should the table load?” box. A different SELECT statement can be written in the box, or the Query Builder tool can be used

16 Creating a Query The query builder tool presents the query. It can be modified and executed and the query results will appear in the Results pane

17 Creating a Query To filter the results, criteria can be typed into the respective Filter column. This builds the optional WHERE clause in the SELECT statement. To order the results, the sort Type column can be set to ascending or descending

18 Creating a Query When finished, the query gets added to the dataset file

19 Parameter queries

20 Parameter Queries A parameter query is a query that contains a parameter marker in place of a criteria’s value. The parameter marker typically used in SQL is the @ (at) symbol followed by the name of the field you are querying.

21 Parameter Queries

22 Parameter Queries To create parameter queries , open the dataset, data set designer, and table adapter , add a new query to get to the What data should the table load?” screen and choose the query builder

23 Parameter Queries Build the parameter placeholders. In the filter column for the field which will be used to select the records, The Query Builder changes the entry in the Filter column to = @fieldname and also adds the WHERE (fieldname clause to the SELECT statement. The Execute Query button will run the query to test it and the Query Parameters dialog box will open where the criteria can be typed in, which will then be executed to display the results

24 Saving a Query For an application to use a query during run time, you will need to save the query and then invoke it from code. To save a query: After testing the Query Builder tool, and back at the “What data should the table load?” box advance to the Choose Methods to Generate screen. Select both methods (Fill and Return), and rename to something meaningful

25 Saving a Query Click the Finish button. The two methods appear in the DataSet Designer window.

26 Using queries

27 Using Queries In the code window for the event associated with the query, reference the table adapter by its name, and then specify the method to use tblBooksTableAdapter.Fill (tblBooksDataSet.tblBooks) If the query uses a parameter, then the parameter must be supplied tblBooksTableAdapter.FillByAuthor(tblBooksDataSet.tblBooks,strAuthor)


Download ppt "CIS16 Application Programming with Visual Basic"

Similar presentations


Ads by Google