SqlDataSource 是 ASP.NET 2.0 內建的資料來源控制項,讓開發者以宣告式標記快速連接 SQL Server、執行查詢並綁定至 GridView、DetailsView 等資料繫結控制項,無需撰寫大量 ADO.NET 程式碼。
web.config 的 <connectionStrings>。<asp:SqlDataSource ID="MySource" runat="server"
ConnectionString="Server=MyServer;Database=Northwind;Integrated Security=true"
SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products" />
若使用 SQL 驗證:
ConnectionString="Server=MyServer;User ID=MyID;Password=Mypass;Database=Northwind"
SelectCommand="SELECT * FROM MyView",DBA 常以 View 限制可存取欄位。SELECT * FROM [My View]。SqlDataSource,設定連線至 (local)\SQLExpress,選擇 Northwind,勾選 Products 資料表的 ProductID、ProductName、UnitPrice 欄位。GridView,選擇資料來源為剛建立的 SqlDataSource,啟用分頁、排序。透過視覺化設計工具,幾分鐘即可完成資料驅動頁面,極大提升開發效率。