Mega Code Archive

 
Categories / ASP.Net Tutorial / ADO Net Database
 

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.Common; 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.CommandText = commandText;         DataTable table = new DataTable();         adapter.Fill(table);         Results.DataSource = table;         Results.DataBind();     } }