Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 2 Working with Visual Studio 2022

Running SQL Queries

The Server Explorer also allows developers to run SQL queries from within Visual Studio. Go ahead, right-click a table (Figure 2-71), and click New Query from the context menu.

Figure 2-71.  Run a SQL query

Note that the context menu changes depending on what item you have rightclicked in the Server Explorer. When right-clicking a table, you see items related to a SQL table. When right-clicking a View, you see items specific to the View, such as Show Results and Open View Definition. The context menu will display the Execute command when right-clicking a Stored Procedure.

Copy the SQL query in Listing 2-27. You will have had to create the table using the CREATE statement in Listing 2-26.

147

Chapter 2 Working with Visual Studio 2022

Listing 2-27.  SQL Select Statement

SELECT

itemName

,category

,price

,priceCategory

FROM menu

When you have pasted the SQL statement (Figure 2-72), execute it by clicking the run button, holding down Ctrl+Shift+E, or executing it with the debugger Alt+F5.

Figure 2-72.  Running a select statement

If you are used to pressing F5 in SQL Server Management Studio, you might find yourself starting the Visual Studio debugger instead of running the query. I find clicking the run button easier to avoid my muscle memory faux pas.

Adding additional items to the table is quickly done by running the INSERT statement in Listing 2-28.

148

Chapter 2 Working with Visual Studio 2022

Listing 2-28.  Insert Statement

INSERT INTO [dbo].[menu] ([itemName],[category],[price],[priceCategory]) VALUES

('bread','breads',2.50,'baker')

If we rerun the SELECT statement, you will see that the entry has been added to the table, as seen in Figure 2-73.

Figure 2-73.  New item inserted

From the results in Figure 2-73, we can see that by adding the priceCategory column, we have a few NULL fields in the menu table. Let’s change that by running the SQL statement in Listing 2-29.

Listing 2-29.  SQL Update Statement

UPDATE menu

SET priceCategory = 'DELI'

WHERE category IN ('meats', 'salads', 'soups')

149