AccessDataSource 构造函数

定义

初始化 AccessDataSource 类的新实例。

重载

AccessDataSource()

初始化 AccessDataSource 类的新实例。

AccessDataSource(String, String)

使用指定数据文件路径和 Select 命令初始化 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)

使用指定数据文件路径和 Select 命令初始化 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 文件的位置。 此位置可以相对于当前 Web 窗体的文件夹、绝对物理路径或虚拟路径。

selectCommand
String

用于从 Access 数据库检索数据的 SQL 查询。 如果 SQL 查询是参数化 SQL 字符串,请将 Parameter 对象添加到 SelectParameters 集合中。

例外

dataFilenull 或空字符串。

示例

下面的代码示例演示如何使用 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>

另请参阅

适用于