SqlDataSource.ProviderName 属性

定义

获取或设置 .NET Framework 数据提供程序的名称,SqlDataSource 控件使用该提供程序来连接基础数据源。Gets or sets the name of the .NET Framework data provider that the SqlDataSource control uses to connect to an underlying data source.

public:
 virtual property System::String ^ ProviderName { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public virtual string ProviderName { get; set; }
member this.ProviderName : string with get, set
Public Overridable Property ProviderName As String

属性值

SqlDataSource 所使用的数据提供程序的名称;如果没有设置任何提供程序,则为 Microsoft SQL Server 的 ADO.NET 提供程序的名称。The name of the data provider that the SqlDataSource uses; otherwise, the ADO.NET provider for Microsoft SQL Server, if no provider is set. 默认值为 Microsoft SQL Server 的 ADO.NET 提供程序的名称。The default is the ADO.NET provider for Microsoft SQL Server.

属性

示例

本部分包含两个代码示例。This section contains two code examples. 第一个代码演示如何使用SqlDataSource控件的 SQL Server 的默认 .NET Framework 数据提供程序连接到 SQL Server 数据库System.Data.SqlClient,。The first code demonstrates how to connect to a SQL Server database using the default .NET Framework Data Provider for SQL Server for the SqlDataSource control, the System.Data.SqlClient. 第二个代码示例演示如何使用适用于 ODBC System.Data.Odbc的 .NET Framework 数据提供程序连接到 odbc 数据库。The second code example demonstrates how to connect to an ODBC database using the .NET Framework Data Provider for ODBC, the System.Data.Odbc.

下面的代码示例演示如何使用SqlDataSource控件System.Data.SqlClient的默认数据提供程序连接到 SQL Server 数据库。The following code example demonstrates how to connect to a SQL Server database using the default data provider for the SqlDataSource control, the System.Data.SqlClient. 只要未显式设置属性,就会使用默认提供程序。ProviderNameWhenever the ProviderName property is not explicitly set, the default provider is used. ConnectionString属性是特定于提供程序的。The ConnectionString property is provider-specific.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </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">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

下面的代码示例在功能上与前面的代码示例相同, 演示了如何使用 ODBC System.Data.Odbc的 .NET Framework 数据提供程序连接到 odbc 数据库。The following code example, which is functionally the same as the preceding code example, demonstrates how to connect to an ODBC database using the .NET Framework Data Provider for ODBC, the System.Data.Odbc. ConnectionString属性设置为用于连接到 odbc 数据库的 odbc 数据源名称 (DSN) 的名称。The ConnectionString property is set to the name of an ODBC data source name (DSN) that is used to connect to the ODBC database.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <!-- This example uses a Northwind database that is hosted by an ODBC-compliant
         database. To run this sample, create an ODBC DSN to any database that hosts
         the Northwind database, including Microsoft SQL Server or Microsoft Access,
         change the name of the DSN in the ConnectionString, and view the page.
    -->
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          ConnectionString="dsn=myodbc3dsn;"
          SelectCommand="SELECT LastName FROM Employees;">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataSourceID="SqlDataSource1"
          DataTextField="LastName">
      </asp:ListBox>

    </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">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <!-- This example uses a Northwind database that is hosted by an ODBC-compliant
         database. To run this sample, create an ODBC DSN to any database that hosts
         the Northwind database, including Microsoft SQL Server or Microsoft Access,
         change the name of the DSN in the ConnectionString, and view the page.
    -->
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          ConnectionString="dsn=myodbc3-test;"
          SelectCommand="SELECT LastName FROM Employees;">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataSourceID="SqlDataSource1"
          DataTextField="LastName">
      </asp:ListBox>

    </form>
  </body>
</html>

注解

.NET Framework 包括以下数据访问接口:The .NET Framework includes the following data providers:

属性永远不会设置为非托管 ADO.NET 提供程序的名称, MSDAORA例如。 ProviderNameThe ProviderName property is never set to the name of an unmanaged ADO.NET provider, such as MSDAORA. 有关详细信息, 请参阅使用 SqlDataSource 控件选择数据For more information, see Selecting Data Using the SqlDataSource Control.

如果更改ProviderName属性DataSourceChanged , 则会引发事件, 从而导致绑定到的SqlDataSource任何控件重新绑定。If you change the ProviderName property, the DataSourceChanged event is raised, causing any controls that are bound to the SqlDataSource to rebind.

在 machine.config 文件的DbProviderFactories system.data节的子节中指定了可用提供程序的列表。A list of available providers is specified in the DbProviderFactories subsection of the system.data section of the Machine.config file.

适用于

另请参阅