SqlDataSource.ProviderName Propriedade

Definição

Obtém ou define o nome do provedor de dados .NET Framework que o controle SqlDataSource usa para se conectar a uma fonte de dados subjacente.

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=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public virtual string ProviderName { get; set; }
[System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public virtual string ProviderName { get; set; }
[<System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.ProviderName : string with get, set
[<System.ComponentModel.TypeConverter("System.Web.UI.Design.WebControls.DataProviderNameConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.ProviderName : string with get, set
Public Overridable Property ProviderName As String

Valor da propriedade

O nome do provedor de dados que o SqlDataSource usa; caso contrário, o provedor ADO.NET para Microsoft SQL Server, se nenhum provedor foi definido. O padrão é o provedor ADO.NET para o Microsoft SQL Server.

Atributos

Exemplos

Esta seção contém dois exemplos de código. O primeiro código demonstra como se conectar a um banco de dados SQL Server usando o provedor de dados .NET Framework padrão para SQL Server para o SqlDataSource controle , o System.Data.SqlClient. O segundo exemplo de código demonstra como se conectar a um banco de dados ODBC usando o provedor de dados .NET Framework para ODBC, o System.Data.Odbc.

O exemplo de código a seguir demonstra como se conectar a um banco de dados SQL Server usando o provedor de dados padrão para o SqlDataSource controle , o System.Data.SqlClient. Sempre que a ProviderName propriedade não for definida explicitamente, o provedor padrão será usado. A ConnectionString propriedade é específica do provedor.

<%@ 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>

O exemplo de código a seguir, que é funcionalmente o mesmo que o exemplo de código anterior, demonstra como se conectar a um banco de dados ODBC usando o provedor de dados .NET Framework para ODBC, o System.Data.Odbc. A ConnectionString propriedade é definida como o nome de um DSN (nome de fonte de dados) ODBC que é usado para se conectar ao banco de dados ODBC.

<!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>

Comentários

O .NET Framework inclui os seguintes provedores de dados:

A ProviderName propriedade nunca é definida como o nome de um provedor de ADO.NET não gerenciado, como MSDAORA. Para obter mais informações, consulte Selecionando dados usando o controle SqlDataSource.

Se você alterar a ProviderName propriedade, o DataSourceChanged evento será acionado, fazendo com que todos os controles associados ao SqlDataSource sejam reacopados.

Uma lista de provedores disponíveis é especificada na DbProviderFactories subseção da system.data seção do arquivo Machine.config.

Aplica-se a

Confira também