ASP.NET Tutorial/ADO.net Database/DbProviderFactories — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:56, 26 мая 2010
By changing the provider name and properly adapting the connection string and command.
<%@ 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>
<div id="pageContent">
<form id="form1" runat="server">
<table>
<tr>
<td><b>Provider</b></td>
<td><asp:TextBox ID="ProviderNameBox" runat="server" Width="500px"
text="System.Data.SqlClient"></asp:TextBox></td>
</tr>
<tr>
<td><b>Connection String</b></td>
<td><asp:TextBox ID="ConnectionStringBox" runat="server" Width="500px"
text="SERVER=(local);DATABASE=northwind;Trusted_Connection=yes;"></asp:TextBox></td>
</tr>
<tr>
<td><b>Command Text</b></td>
<td><asp:TextBox ID="CommandTextBox" runat="server" Width="500px"
text="SELECT TOP 15 customerid, companyname FROM customers"></asp:TextBox></td>
</tr>
</table>
<asp:Button runat="server" ID="RunButton" Text="Run" OnClick="RunButton_Click" />
<hr />
<asp:datagrid runat="server" id="Results">
<HeaderStyle Font-Bold="true" />
</asp:datagrid>
</form>
</div>
</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();
}
}
Provider is characterized by an invariant name, a description, and a type that contains assembly and class information.
<%@ 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>
<div id="pageContent">
<form id="form1" runat="server">
<asp:datagrid runat="server" id="provList" Font-Size="8pt">
<HeaderStyle Font-Bold="true" />
</asp:datagrid>
</form>
</div>
</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();
}
}