Microsoft Visual C#: Introduction to Data Selection
After creating a table and populating it with records, you may want to see what you can do with data. One of the most commonly performed operations by the…
Fundamentals of Data Selection#
Introduction#
Once a table is created and filled with records, you might want to explore the possibilities for interacting with that data. A frequent task for database users is searching for or isolating information that meets specific criteria, a process known as querying. The resulting set of data is called a query. As a developer, you execute queries by providing specific instructions to the database engine using reserved keywords. Data selection can be performed through several tools, including the Microsoft SQL Server Management Studio, query windows within both SQL Server Management Studio and Microsoft Visual Studio, or directly via a Windows application.
The Data in the Table Window#
To visually analyze data in Microsoft Visual Studio, in the Server Explorer, you can right-click a table and click Show Table Data. By default, when you open a table, the Query Designer toolbar comes up also:
Once the table is opened, on the main menu, you can click Query Designer. Alternatively, you can right-click anywhere on the table. In both cases
- On the menu that appears, position the mouse on Pane and click Diagram
- Once again, open the Pane menu and click Criteria
- Again, open the Pane menu and click SQL:
Alternatively, on the Query Designer toolbar, you can click the Show Diagram Pane button , the Show Criteria Pane button , and the Show SQL Pane button .
The Table window is divided in four sections:
- Diagram: The top section is referred to as the Diagram window. It displays the table(s) that contain(s) the columns you want to query. Each column displays a check box on its left and the name of the column on the right. The first item of the list has a name made of an asterisk and the (All Columns) expression. If the list of items is too long for the allocated rectangle to display, the table would be equipped with a vertical scroll bar.
- Criteria: Under the table, the second section is called Criteria. It displays a list of columns used to visually build the SQL statement.
- SQL: The third section from top, called SQL, displays the SQL statement that results from selections in the Diagram or the Criteria sections
- Results: The bottom section, called Results, displays the result produced by the SQL statement when it is executed
If you do not want a particular section or you want to hide some sections, you can right-click anywhere in the table, position the mouse on Pane and click the name of the section. To hide any section, you can right-click anywhere in the window, position the mouse on Show Panes and click one of the selections:
When a section is displaying, its menu option is surrounded.
Column Selection#
The primary way to construct a SQL statement is by choosing one or more columns you wish to display. In the Diagram area of the Table window, you can select a column by checking the box next to its name:
After clicking the check box of a column, it becomes selected in the Criteria section also and its name appears in the SQL section. Another technique used to select a column consists of clicking a box under the Column header of the Criteria section. This would reveal that it is a combo box. You can then click the arrow of the combo box to display the list and select a column from that list:
In the Criteria section, if you click a combo box that already contains a column but select another, the previous one would be replaced by the new one. Also, after selecting a column in the Criteria section, its check box becomes selected in the Diagram section and its name gets added to the SQL expression. If you know the name of a column that you want to add, which you can see in the Diagram section, you can directly enter it in the SQL statement. Any of the above three techniques allows you to select one or more columns to build the desired SQL statement.
SQL Statement Execution#
After creating a SQL statement, you can view its result, which you can get by executing the statement. To do this, you can right-click anywhere in the Table window and click Execute SQL. Alternatively, on the Query Designer toolbar, you can click the Execute SQL button .
The Results section at the bottom is populated with data from the chosen columns once the statement is executed. Here is an illustration:
The Data in the Table Window#
To visually analyze data in Microsoft Visual Studio, in the Server Explorer, you can right-click a table and click Show Table Data. By default, when you open a table, the Query Designer toolbar comes up also:
Once the table is opened, on the main menu, you can click Query Designer. Alternatively, you can right-click anywhere on the table. In both cases
- On the menu that appears, position the mouse on Pane and click Diagram
- Once again, open the Pane menu and click Criteria
- Again, open the Pane menu and click SQL:
Alternatively, on the Query Designer toolbar, you can click the Show Diagram Pane button , the Show Criteria Pane button , and the Show SQL Pane button .
The Table window is divided in four sections:
- Diagram: The top section is referred to as the Diagram window. It displays the table(s) that contain(s) the columns you want to query. Each column displays a check box on its left and the name of the column on the right. The first item of the list has a name made of an asterisk and the (All Columns) expression. If the list of items is too long for the allocated rectangle to display, the table would be equipped with a vertical scroll bar.
- Criteria: Under the table, the second section is called Criteria. It displays a list of columns used to visually build the SQL statement.
- SQL: The third section from top, called SQL, displays the SQL statement that results from selections in the Diagram or the Criteria sections
- Results: The bottom section, called Results, displays the result produced by the SQL statement when it is executed
If you do not want a particular section or you want to hide some sections, you can right-click anywhere in the table, position the mouse on Pane and click the name of the section. To hide any section, you can right-click anywhere in the window, position the mouse on Show Panes and click one of the selections:
When a section is displaying, its menu option is surrounded.
Column Selection#
A SQL statement is primarily built by selecting one or more columns whose data you want to view. To select a column, in the Diagram section of the Table window, you can click the check box on the left side of the name:
After clicking the check box of a column, it becomes selected in the Criteria section also and its name appears in the SQL section. Another technique used to select a column consists of clicking a box under the Column header of the Criteria section. This would reveal that it is a combo box. You can then click the arrow of the combo box to display the list and select a column from that list:
In the Criteria section, if you click a combo box that already contains a column but select another, the previous one would be replaced by the new one. Also, after selecting a column in the Criteria section, its check box becomes selected in the Diagram section and its name gets added to the SQL expression. If you know the name of a column that you want to add, which you can see in the Diagram section, you can directly enter it in the SQL statement. Any of the above three techniques allows you to select one or more columns to build the desired SQL statement.
SQL Statement Execution#
After creating a SQL statement, you can view its result, which you can get by executing the statement. To do this, you can right-click anywhere in the Table window and click Execute SQL. Alternatively, on the Query Designer toolbar, you can click the Execute SQL button .
After executing the statement, the bottom section gets filled with data from only the selected column(s) of the table. Here is an example:
Home Copyright © 2010-2011 C# Key

