AccessDataSource 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 AccessDataSource 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| AccessDataSource() |
初始化 AccessDataSource 類別的新執行個體。 |
| AccessDataSource(String, String) |
使用指定的數據文件路徑,並 AccessDataSource 命令,初始化 類別的新實例。 |
AccessDataSource()
初始化 AccessDataSource 類別的新執行個體。
public:
AccessDataSource();
public AccessDataSource();
Public Sub New ()
範例
以下程式碼範例示範如何使用 AccessDataSource 建構子建立新的 AccessDataSource 資料來源控制,並將 CheckBoxList 控制項綁定到 Microsoft Access 資料庫中的資料。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(Object sender, EventArgs e) {
// Create the AccessDataSource.
AccessDataSource accessDS = new AccessDataSource();
accessDS.SelectCommand = "SELECT SupplierID, CompanyName " +
" FROM Suppliers WHERE Country ='Germany'";
accessDS.DataFile = "~/App_Data/Northwind.mdb";
// Add the AccessDataSource to the Page.Controls collection.
Page.Controls.Add(accessDS);
// In programmatic scenarios, use the DataSource
// property, not the DataSourceID property. The Select method
// returns an IEnumerable list of data items.
CheckBoxList1.DataSource = accessDS;
// Explicitly call DataBind.
CheckBoxList1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList
id="CheckBoxList1"
runat="server"
DataTextField="CompanyName"
DataValueField="SupplierID">
</asp:CheckBoxList>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create the AccessDataSource.
Dim accessDS As New AccessDataSource()
accessDS.SelectCommand = "SELECT SupplierID, CompanyName " & _
" FROM Suppliers WHERE Country ='Germany'"
accessDS.DataFile = "~/App_Data/Northwind.mdb"
' Add the AccessDataSource to the Page.Controls collection.
Page.Controls.Add(accessDS)
' In programmatic scenarios, use the DataSource
' property, not the DataSourceID property. The Select method
' returns an IEnumerable list of data items.
CheckBoxList1.DataSource = accessDS
' Explicitly call DataBind.
CheckBoxList1.DataBind()
End Sub 'Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList
id="CheckBoxList1"
runat="server"
DataTextField="CompanyName"
DataValueField="SupplierID">
</asp:CheckBoxList>
</form>
</body>
</html>
另請參閱
適用於
AccessDataSource(String, String)
使用指定的數據文件路徑,並 AccessDataSource 命令,初始化 類別的新實例。
public:
AccessDataSource(System::String ^ dataFile, System::String ^ selectCommand);
public AccessDataSource(string dataFile, string selectCommand);
new System.Web.UI.WebControls.AccessDataSource : string * string -> System.Web.UI.WebControls.AccessDataSource
Public Sub New (dataFile As String, selectCommand As String)
參數
- dataFile
- String
Access .mdb 檔案的位置。 位置可以是相對於目前網頁表單的資料夾、絕對實體路徑,或虛擬路徑。
- selectCommand
- String
用於從 Access 資料庫擷取資料的 SQL 查詢。 如果 SQL 查詢是參數化的 SQL 字串,請將物件加入 Parameter 集合 SelectParameters 。
例外狀況
dataFile 是 null 空字串。
範例
以下程式碼範例示範如何使用 AccessDataSource 建構子建立新的 AccessDataSource 資料來源控制,並將 CheckBoxList 控制項綁定到 Microsoft Access 資料庫中的資料。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(Object sender, EventArgs e) {
// Create AccessDataSource
AccessDataSource accessDS =
new AccessDataSource("~/App_Data/Northwind.mdb",
"SELECT SupplierID, CompanyName " +
" FROM Suppliers WHERE Country ='Germany'");
// Add the AccessDataSource to the Page.Controls collection
Page.Controls.Add(accessDS);
// In programmatic scenarios, use the DataSource
// property, not the DataSourceID property. The Select method
// returns an IEnumerable list of data items.
CheckBoxList1.DataSource = accessDS;
// Explicitly call DataBind
CheckBoxList1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList
id="CheckBoxList1"
runat="server"
DataTextField="CompanyName"
DataValueField="SupplierID">
</asp:CheckBoxList>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create AccessDataSource
Dim accessDS As New AccessDataSource("~/App_Data/Northwind.mdb", _
"SELECT SupplierID, CompanyName " & _
" FROM Suppliers WHERE Country ='Germany'")
' Add the AccessDataSource to the Page.Controls collection
Page.Controls.Add(accessDS)
' In programmatic scenarios, use the DataSource
' property, not the DataSourceID property. The Select method
' returns an IEnumerable list of data items.
CheckBoxList1.DataSource = accessDS
' Explicitly call DataBind
CheckBoxList1.DataBind()
End Sub 'Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList
id="CheckBoxList1"
runat="server"
DataTextField="CompanyName"
DataValueField="SupplierID">
</asp:CheckBoxList>
</form>
</body>
</html>