TAILIEUCHUNG - ASP.NET 4 Unleased - p 47

Using the SqlDataSource Control LISTING using using using using using ; ; ; ; ; /// /// Displays an image corresponding to the Id passed /// in a query string field /// public class DynamicImage : IHttpHandler { public void ProcessRequest (HttpContext context) { // Get the Id of the image to display string imageId = [“Id”]; // Use SqlDataSource to grab image bytes SqlDataSource src = new SqlDataSource(); = [“Images”].ConnectionString; = “SELECT Image FROM Images WHERE Id=” + imageId; // Return a DataView DataView view = (DataView)(); ( (byte[])view[0][“Image”]); // Return a DataReader. | 434 CHAPTER 9 Using the SqlDataSource Control LISTING @ WebHandler Language C Class DynamicImage using using using using using summary Displays an image corresponding to the Id passed in a query string field summary public class Dynamicimage IHttpHandler public void ProcessRequest HttpContext context Get the Id of the image to display string imageId Id Use SqlDataSource to grab image bytes SqlDataSource src new SqlDataSource Images .ConnectionString SELECT Image FROM Images WHERE Id imageId Return a DataView DataView view DataView byte view 0 Image Return a DataReader IDataReader reader IDataReader byte reader Image public bool IsReusable From the Library of Wow eBook Caching Database Data with the SqlDataSource Control 435 get return false In the ProcessRequest method an instance of the SqlDataSource control is created. The SqlDataSource control s Connectionstring and SelectCommand properties are initialized. Finally the SqlDataSource control s Select command is executed and the results are rendered with the method. The return value from the Select method is cast explicitly to a DataView object. You need to cast the return value to either a DataView or IDataReader for it to work with the results of the Select method. In Listing the image bytes are returned in a DataView. To illustrate how you can use the Select method to return a DataReader I also included the code for returning the image with a DataReader but I added comments to the code so that it won t execute. Caching Database Data with the SqlDataSource .

Đã 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.