Executing Multiple SQL Statements

Executing Multiple SQL Statements Typically, your C# program and the database will run on different computers and communicate over a network. Each time you execute a command in your program | Executing Multiple SQL Statements Typically your C program and the database will run on different computers and communicate over a network. Each time you execute a command in your program it has to travel over the network to the database and be executed by the database and any results must be sent back across the network to your program. That s a lot of network traffic One way to potentially reduce network traffic is to execute multiple SQL statements at a time. In this section you ll see how to execute multiple SELECT statements and retrieve results and you ll see how to execute multiple SELECT INSERT UPDATE and DELETE statements that are interleaved. Executing Multiple SELECT Statements Let s take a look at how you execute multiple SELECT statements and retrieve the results. The following code first creates a SqlCommand object named mySqlCommand and sets its CommandText property to three different SELECT statements SqlCommand mySqlCommand SELECT TOP 5 ProductID ProductName FROM Products ORDER BY ProductID SELECT TOP 3 CustomerID CompanyName FROM Customers ORDER BY CustomerID SELECT TOP 6 OrderID CustomerID FROM Orders ORDER BY OrderID Notice that all of the SELECT statements are separated by semi-colons. Using Table Joins Be careful to retrieve only the rows and columns you actually need. Also make sure you use SELECT statements that retrieve rows from multiple tables. For example if you want to see all the orders placed by the customer with the CustomerID of ALFKI don t perform two separate SELECT statements against the Customers and Orders tables. Instead use a table join as shown in the following SELECT statement SELECT CompanyName OrderlD FROM Customers Orders WHERE AND ALFKI To run earlier SQL statements you call the ExecuteReader method which returns a SqlDataReader object SqlDataReader mySqlDataReader .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU LIÊN QUAN
5    176    1
5    255    1
5    106    0
5    121    1
6    103    1
6    107    1
6    121    1
6    103    0
6    140    0
6    98    0
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.