Đang chuẩn bị liên kết để tải về tài liệu:
ASP.NET 4 Unleased - p 91

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Trả về một giá trị đơn Nếu bạn cần phải trả lại một giá trị duy nhất từ một truy vấn cơ sở dữ liệu, bạn có thể sử dụng SqlCommand.ExecuteScalar () phương pháp. Phương pháp này luôn luôn trả về giá trị của cột đầu tiên từ dòng đầu tiên của một tập kết quả. Ngay cả khi một truy vấn trả lại hàng trăm cột và hàng tỷ hàng, mọi thứ đều bị bỏ qua trừ giá trị của cột đầu tiên của hàng đầu tiên | 874 CHAPTER 19 Building Data Access Components with ADO.NET div form body html Returning a Single Value If you need to return a single value from a database query you can use the SqlCommand.ExecuteScalar method. This method always returns the value of the first column from the first row of a resultset. Even when a query returns hundreds of columns and billions of rows everything is ignored except for the value of the first column from the first row. For example the page in Listing 19.17 contains a lookup form. If you enter the title of a movie the movie s total box office returns display in a Label control see Figure 19.8 . FIGURE 19.8 Retrieving a value with ExecuteScalar . LISTING 19.17 ShowExecuteScalar.aspx @ Page Language C @ Import Namespace System.Data @ Import Namespace System.Data.SqlClient @ Import Namespace System.Web.Configuration DOCTYPE html PUBLIC - W3C DTD XHTML 1.0 Transitional EN From the Library of Wow eBook Connected Data Access 875 http www.w3.org TR xhtml1 DTD xhtml1-transitional.dtd script runat server protected void btnSearch_Click object sender EventArgs e string connectionstring WebConfigurationManager.ConnectionStrings Movies .ConnectionString SqlConnection con new SqlConnection connectionString SqlCommand cmd new SqlCommand SELECT BoxOfficeTotals FROM Movies WHERE Title @Title con cmd.Parameters.AddWithValue @Title txtTitle.Text using con con.Open Object result cmd.ExecuteScalar if result null lblResult.Text String.Format 0 c result else lblResult.Text No match script html xmlns http www.w3.org 1999 xhtml head id Head1 runat server title Show Execute Scalar title head body form id form1 runat server div h1 Box Office Totals h1 asp Label id lblTitle Text Movie Title AssociatedControlID txtTitle Runat server asp TextBox id txtTitle Runat server asp Button id btnSearch 19 From the Library of Wow eBook 876 CHAPTER 19 Building Data Access Components with ADO.NET Text Search OnClick btnSearch_Click Runat server hr asp Label id lblResult Runat

TÀI LIỆU LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.