ASP.NET Tutorial/ADO.net Database/DbProviderFactories
By changing the provider name and properly adapting the connection string and command.
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Agnostic</title>
</head> <body>
</div>
<form id="form1" runat="server">
Provider | <asp:TextBox ID="ProviderNameBox" runat="server" Width="500px" text="System.Data.SqlClient"></asp:TextBox> |
Connection String | <asp:TextBox ID="ConnectionStringBox" runat="server" Width="500px" text="SERVER=(local);DATABASE=northwind;Trusted_Connection=yes;"></asp:TextBox> |
Command Text | <asp:TextBox ID="CommandTextBox" runat="server" Width="500px" text="SELECT TOP 15 customerid, companyname FROM customers"></asp:TextBox> |
<asp:Button runat="server" ID="RunButton" Text="Run" OnClick="RunButton_Click" />
<asp:datagrid runat="server" id="Results"> <HeaderStyle Font-Bold="true" /> </asp:datagrid> </form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Data.rumon; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page {
protected void RunButton_Click(object sender, EventArgs e) { string provider = ProviderNameBox.Text; string connString = ConnectionStringBox.Text; string commandText = CommandTextBox.Text; DbProviderFactory fact = DbProviderFactories.GetFactory(provider); DbConnection conn = fact.CreateConnection(); conn.ConnectionString = connString; DbDataAdapter adapter = fact.CreateDataAdapter(); adapter.SelectCommand = conn.CreateCommand(); adapter.SelectCommand.rumandText = commandText; DataTable table = new DataTable(); adapter.Fill(table); Results.DataSource = table; Results.DataBind(); }
}</source>
Provider is characterized by an invariant name, a description, and a type that contains assembly and class information.
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Listing Factory Objects</title>
</head> <body>
<form id="form1" runat="server"> <asp:datagrid runat="server" id="provList" Font-Size="8pt"> <HeaderStyle Font-Bold="true" /> </asp:datagrid> </form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Data.rumon; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { DataTable providers = DbProviderFactories.GetFactoryClasses(); provList.DataSource = providers; provList.DataBind(); }
}</source>