14.4. Database Queries
Okay. . . so now you know how to create a database and its associated tables, add fields to a table, and add or update a record in a table. How do you retrieve the information in a useful way? Obviously, that's the "query" part of SQL. Figure 14-8 shows the form for generating a query.
Figure 14-8. Figure 14-8
The figure shows the state of the form after the Do Query menu option is selected. The form cannot be loaded if the user has not opened a database. The code that ensures the database is open is in the mnuQuery_Click() event code in frmMain:
private void mnuQuery_Click(object sender, EventArgs e) { if (dbName == null) // See if they opened a DB { MessageBox.Show("You must open a database first"); return; } frmReport myFriendReport = new frmReport(dbName, tableName); myFriendReport.ShowDialog(); }
If variable dbName is not set, a message is issued asking the user to select a database. Assuming that he has selected a database, the form shown in Figure 14-8 appears. The listbox object on the left side of the figure lists the tables that are in the currently opened database. When the user double-clicks one of the table names, the code performs a nonrestrictive query on the database based upon the table selected. The SELECT statement is copied into the textbox and the query is run. A sample result is shown in Figure 14-9.
Figure 14-9. Figure 14-9
One ...
Get Beginning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.