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

C#高級編程:選擇數據[1]

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

    定義了數據連接後就可以從可用的列表中選擇一個表(或視圖)把該表從項目中拖放到活動的窗體上如圖所示


圖 

    在本例中選擇Customers表把這個對象拖放到項目中時(可以把它拖放到窗體上或者拖放到服務器控件面板上)就會在窗體上添加一個派生於SqlDataAdapter或 OleDbDataAdaptor的對象(如果沒有使用SQL Server)

    生成的數據適配器包含SELECTINSERTUPDATE和DELETE命令顯然也可以調用存儲過程而不直接使用SQL命令向導生成的代碼也可以完成這個工作Visual Studio NET給cs文件添加如下代碼
    private SystemDataSqlClientSqlCommand sqlSelectCommand;
    private SystemDataSqlClientSqlCommand sqlInsertCommand;
    private SystemDataSqlClientSqlCommand sqlUpdateCommand;
    private SystemDataSqlClientSqlCommand sqlDeleteCommand;
    private SystemDataSqlClientSqlDataAdapter sqlDataAdapter;

    為每個SQL命令定義一個對象和一個SqlDataAdapter在文件後面的InitializeComponent方法中向導生成了創建這些命令和數據適配器的代碼

    在Visual Studio NET生成的代碼中需要解釋兩個地方UpdateCommand和InsertCommand屬性下面是顯示有關信息的節選版本
    //
    // sqlInsertCommand
    //
    thissqlInsertCommandCommandText = @INSERT INTO Customers
                                    (CustomerID CompanyName ContactName
                                    ContactTitle Address City Region
                                    PostalCode Country Phone Fax)
          VALUES(@CustomerID @CompanyName @ContactName @ContactTitle
                 @Address @City @Region @PostalCode @Country @Phone @Fax);
          SELECT CustomerID CompanyName ContactName ContactTitle Address
                 City Region PostalCode Country Phone Fax
          FROM Customers WHERE (CustomerID = @CustomerID);
    thissqlInsertCommandConnection = thissqlConnection;       
    thissqlInsertCommandParamatersAdd(
          new SystemDataSqlClientSqlParamater(@CustomerID
              SystemDataSqlDbTypeNVarChar CustomerID));       
    // Other Paramaters omitted for clarity       
    //
    // sqlUpdateCommand
    //       
    thissqlUpdateCommandCommandText = @UPDATE Customers
                  SET CustomerID = @CustomerID CompanyName = @CompanyName
                      ContactName = @ContactName ContactTitle = @ContactTitle
                      Address = @Address City = @City Region = @Region
                      PostalCode = @PostalCode Country = @Country

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/15420.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.