首页 > Java开发 > 只需修改txt文档就能连接两种数据库(利用继承和多态)

只需修改txt文档就能连接两种数据库(利用继承和多态)

Java开发中有时需要连接两个数据库,则可以修改指定路径的txt文本文档数据,也可以在Web.config中修改key的值来控制要连接的数据库

前台代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click1" />
<asp:Button ID="btnedit" runat="server" onclick="btnedit_Click" Text="保存" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="编号" />
<asp:BoundField DataField="NAME" HeaderText="帐号" />
<asp:BoundField DataField="PASSWORD" HeaderText="密码" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click" CommandArgument='<%#Eval("ID") %>'>删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="ledelete" runat="server" CommandArgument='<%#Eval("ID") %>'  onclick="ledelete_Click">编辑</asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>
</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.Configuration;
using System.Data.OracleClient;
using System.Data;
using System.Text;
using System.Data.Common;
using System.IO;
using System.Data.SqlClient;

namespace Orcale连接数据库
{
public partial class WebForm1 : System.Web.UI.Page
{
//string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataLoad();
}
}
private void DataLoad()
{
DbConnection conn = GetCon();
DbCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "select * from T_USER order by ID";
DbDataAdapter adapter = GetDataadpter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DbDataAdapter GetDataadpter()
{
DbDataAdapter adapter = null;
//string content = File.ReadAllText(@"F:\vs第四学期\Orcale连接数据库\111.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 constr = "";
DbConnection conn = null;
//string content = File.ReadAllText(@"F:\vs第四学期\Orcale连接数据库\111.txt");
string content = ConfigurationManager.AppSettings["data"];
if (content.ToUpper()=="SQLSERVER")
{
constr = ConfigurationManager.ConnectionStrings["sqlconstr"].ConnectionString;
conn = new SqlConnection(constr);
}
else if (content.ToUpper() == "ORACLE")
{
constr = ConfigurationManager.ConnectionStrings["oracleconstr"].ConnectionString;
conn = new OracleConnection(constr);
}
return conn;
}

private DbCommand Getcmd()
{
DbCommand cmd = null;
//string content = File.ReadAllText(@"F:\vs第四学期\Orcale连接数据库\111.txt");
string content = ConfigurationManager.AppSettings["data"];
if (content.ToUpper() == "SQLSERVER")
{
cmd = new SqlCommand();
cmd.Connection.Open();
}
else if (content.ToUpper() == "ORACLE")
{
cmd = new SqlCommand();
}
return cmd;
}

}
}

Web.config代码

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="data" value="ORACLE"/>

</appSettings>
<connectionStrings>
<add name="oracleconstr" connectionString="Data Source=(oracle数据库);Persist Security Info=True;User ID=(帐号);Password=(密码);Unicode=True" providerName="System.Data.OracleClient"/>
<add name="sqlconstr" connectionString="Data Source=.;initial catalog=Food;User ID=sa;Password=(密码);"/>

</connectionStrings>
</configuration>


本文固定链接: http://www.devba.com/index.php/archives/1019.html | 开发吧

报歉!评论已关闭.