熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#高級編程: 數據庫的綁定[2]

2013-11-13 10:51:26  來源: .NET編程 
    ——此文章摘自《C#高級編程(第版)》定價元 特價元 購買

       public class WebForm : SystemWebUIPage
       {
         
          protected SystemDataDataSet ds;
          protected SystemDataDataTable eventTable;
          protected SystemDataOleDbOleDbDataAdapter daAttendees;
          protected SystemDataOleDbOleDbDataAdapter daRooms;
          protected SystemDataOleDbOleDbDataAdapter daEvents;

    提示所有OLE DB對象都有SQL Server版本它們的用法也是一樣的

    Page_Load()現在需要創建DataSet對象
          private void Page_Load(object sender SystemEventArgs e)
          {
             oleDbConnectionOpen();
             ds = new DataSet();

    然後必須給OleDbDataAdapter對象賦予查詢和連接對象的鏈接
             ds = new DataSet();
             daAttendees = new SystemDataOleDbOleDbDataAdapter(
                               SELECT * FROM Attendees oleDbConnection);
             daRooms = new SystemDataOleDbOleDbDataAdapter(
                               SELECT * FROM Rooms oleDbConnection);
             daEvents = new SystemDataOleDbOleDbDataAdapter(
                               SELECT * FROM Events oleDbConnection);

    使用Fill()調用來執行查詢
             daEvents = new SystemDataOleDbOleDbDataAdapter(
                               SELECT * FROM Events oleDbConnection);
             daAttendeesFill(ds Attendees);
             daRoomsFill(ds Rooms);
             daEventsFill(ds Events);

    現在進行數據綁定如前所述只需把綁定控件上的DataSource屬性設置為要綁定的表
             daEventsFill(ds Events);
             attendeeListDataSource = dsTables[Attendees];
             roomListDataSource = dsTables[Rooms];

    這段代碼設置了屬性但數據綁定要在調用窗體的DataBind()方法之後才進行該方法過一會兒再調用在此之前用Events表中的數據填充DataTable對象
             roomListDataSource = dsTables[Rooms];
             eventTable = dsTables[Events];

    如果沒有回送就只綁定數據否則就要刷新數據(假定數據庫中的這些數據在會議登記請求的過程中一直是靜態的)回送中的數據綁定也會清除roomList 和 attendeeList控件中的選擇在綁定並刷新這些數據前應注意這些但是在現有的if語句中調用DataBind()會比較簡單(這是該語句仍保留在打開數據連接的代碼區域中的原因)
             eventTable = dsTables[Events];
             if (!thisIsPostBack)
             {
                calendarSelectedDate = SystemDateTimeNow;
                thisDataBind();
             }
             oleDbConnectionClose();
          }

    現在運行這個應用程序從數據綁定控件中得到所有可用的參加者和會議室數據

    注意為了運行代碼必須在Access或Server Explorer中明確關閉任何已打開的數據庫連接為此只需右擊Server Explorer中的數據源選擇Close即可

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/14643.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.