--------前台--------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="接口.WebForm1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" GridLines="None" Height="230px" Width="471px"
ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:BoundField DataField="Password" HeaderText="密码" />
<asp:BoundField DataField="E_Mail" HeaderText="邮件" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<br />
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
-----------后台-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Data.OracleClient;
using System.Configuration;
namespace 接口
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataLoad();
}
}
private void DataLoad()
{
DbConnection conn = GetCon();
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_User";
DataTable dt = new DataTable();
DbDataAdapter adapter = GetDataadpter();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
//根据选择的数据库动态返回数据适配器
private DbDataAdapter GetDataadpter()
{
DbDataAdapter adapter = null;
//string content = File.ReadAllText(@"c:\conncetion.txt");
string content = ConfigurationManager.AppSettings["data"];
if (content.ToUpper() == "SQLSERVER")
{
adapter = new SqlDataAdapter();
}
else if (content.ToUpper() == "ORACLE")
{
adapter = new OracleDataAdapter();
}
return adapter;
}
//根据选择的数据库动态返回练级对象
private DbConnection GetCon()
{
string strcon = "";
DbConnection con = null;
// string content = File.ReadAllText(@"c:\conncetion.txt");
string content=ConfigurationManager.AppSettings["data"];
if (content.ToUpper()=="SQLSERVER")
{
strcon = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
con = new SqlConnection(strcon);
}
else if (content.ToUpper() == "ORACLE")
{
strcon = ConfigurationManager.ConnectionStrings["oraclecon"].ConnectionString; ;
con = new OracleConnection(strcon);
}
return con;
}
protected void Button1_Click(object sender, EventArgs e)
{
DbConnection conn = GetCon();
conn.Open();
DbCommand cmd = conn.CreateCommand();
string sqlrt= "insert into T_User(Id,Name,Password,E_Mail) VALUES(@id,@name,@password,@email)";
if (conn.GetType() == typeof(OracleConnection))
{
sqlrt= sqlrt.Replace('@', ':');
cmd.CommandText = sqlrt;
cmd.Parameters.Add(new OracleParameter(":id", Convert.ToInt32(txtId.Text)));
cmd.Parameters.Add(new OracleParameter(":name", txtName.Text));
cmd.Parameters.Add(new OracleParameter(":password", txtPassword.Text));
cmd.Parameters.Add(new OracleParameter(":email", txtEmail.Text));
}
else
{
cmd.CommandText = sqlrt;
cmd.Parameters.Add(new SqlParameter("@id", Convert.ToInt32(txtId.Text)));
cmd.Parameters.Add(new SqlParameter("@name", txtName.Text));
cmd.Parameters.Add(new SqlParameter("@password", txtPassword.Text));
cmd.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
}
int num = cmd.ExecuteNonQuery();
if (num > 0)
{
Response.Write("插入成功");
DataLoad();
}
}
}
}
------web.config------
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="data" value="SQLSERVER"/>
</appSettings>
<connectionStrings>
<add name="sqlservercon" connectionString="Data Source=LOVE-PC\SQLEXPRESSPC;Initial Catalog=BoKe;Persist Security Info=True;User ID=sa;Password=admin"/>
<add name="oraclecon" connectionString="Data Source=orcl;Persist Security Info=True;User ID=wxj;Password=wxj;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>
</configuration>