首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Visual Studio 2005 開發 ASP.NET 2.0 資料庫應用:伺服器總管管理、TableAdapter 嚮導與聲明式資料繫結

     編輯:關於ASP.NET

Visual Studio 2005 整合資料庫管理工具,配合 ASP.NET 2.0 新世代資料源控制項(SqlDataSource、ObjectDataSource)與資料繫結控制項(GridView、DetailsView、FormView),實現「零程式碼」資料庫應用開發。

VS 2005 伺服器總管資料庫管理

  • 資料連線節點:右鍵「加入連線」輸入伺服器名稱(如 .\SQLEXPRESS)、認證、資料庫。
  • 建立新資料庫:右鍵資料連線 → 「建立新 SQL Server 資料庫」。
  • 資料表/預存程序/檢視表操作:展開資料夾右鍵「加入新資料表」,雙擊現有物件編輯結構,右鍵「顯示資料表資料」瀏覽/編輯資料列。
  • SQL Server 2005 Express 管理:無獨立管理工具,全賴 VS 2005 伺服器總管;安裝完整版 SQL 2005 可使用 Management Studio 同步管理 Express 實例。

TableAdapter 設定嚮導快速產生資料存取層

  1. 專案加入「資料集」項目(.xsd)。
  2. 從伺服器總管拖曳資料表至設計介面,自動產生 TableAdapter、DataTable、TableAdapterManager。
  3. 嚮導引導設定 Select、Insert、Update、Delete 命令,支援參數化查詢與預存程序。
  4. 產生強型別 DataSet 類別,提供 IntelliSense、編譯期型別檢查。

聲明式資料繫結:GridView + SqlDataSource 範例

<asp:SqlDataSource ID="ProductsDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:NWConnectionString %>"
    SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products"
    DeleteCommand="DELETE FROM Products WHERE ProductID = @ProductID"
    UpdateCommand="UPDATE Products SET ProductName=@ProductName, UnitPrice=@UnitPrice WHERE ProductID=@ProductID"
    InsertCommand="INSERT INTO Products (ProductName, UnitPrice) VALUES (@ProductName, @UnitPrice)">
</asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" DataSourceID="ProductsDataSource"
    DataKeyNames="ProductID" AutoGenerateColumns="False"
    AllowPaging="True" AllowSorting="True" AllowEditing="True" AllowDeleting="True">
  <Columns>
    <asp:BoundField DataField="ProductID" HeaderText="ID" ReadOnly="True" SortExpression="ProductID" />
    <asp:BoundField DataField="ProductName" HeaderText="產品名稱" SortExpression="ProductName" />
    <asp:BoundField DataField="UnitPrice" HeaderText="單價" SortExpression="UnitPrice" DataFormatString="{0:C}" />
    <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
  </Columns>
</asp:GridView>

關鍵優勢

  • 無需撰寫 ADO.NET 連線、命令、DataReader/DataSet 填充、手動繫結程式碼。
  • 分頁、排序、編輯、刪除內建支援,屬性啟用即可。
  • 參數自動對應:DataKeyNames 對應更新/刪除鍵值,模板欄位雙向繫結自動收集輸入值。
  • 連接字串集中管理於 web.config <connectionStrings>,部署環境切換零修改程式碼。

進階:ObjectDataSource 分層架構

大型專案建議使用 ObjectDataSource 繫結中間層業務元件(DAL/BLL),分離關注點,利於測試與維護。TableAdapter 產生的類別即可作為資料存取層元件。

VS 2005 與 ASP.NET 2.0 資料工具將資料庫開發從繁瑣程式碼解放,讓開發者專注於業務邏輯與使用者體驗設計。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved