用EnterprisLibrary访问数据库,代码如下:
/// <summary>
/// TestTime 的摘要说明
/// </summary>
public class TestTime
{
string constr = "Tourism";
public TestTime()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 创建数据库用户
/// </summary>
public string CreateDataUser()
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("create user TestUser identified by TestUser default Tablespace users Temporary tablespace temp");
StringBuilder rightstr = new StringBuilder();
rightstr.AppendLine("grant dba to TestUser");
//sqlstr.AppendLine(" grant connect,resource to TestUser");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
db.ExecuteNonQuery(CommandType.Text, rightstr.ToString());
return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 创建表
/// </summary>
public string CreateTable()
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("create table TestUser.TestTable(testfield1 varchar2(100),testfield2 varchar2(100))");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 添加记录
/// </summary>
public void AddTableData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("insert into TestUser.TestTable(testfield1,testfield2) values('" + rowid + "','测试2')");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 更新记录
/// </summary>
/// <param name="rowid"></param>
public void UpdateData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("update TestUser.TestTable set testfield2='更改的测试2' where testfield1='" + rowid + "'");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
// return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="rowid"></param>
public void DelTableData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("delete TestUser.TestTable where testfield1='" + rowid + "'");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
// return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 删除表
/// </summary>
public string DelTable()
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("drop table TestUser.TestTable");
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 删除数据库用户
/// </summary>
public string DelDataUser()
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("drop user TestUser");
//sqlstr.AppendLine("drop user TestUser cascade");//删除该用户的同时删除用户拥有的所有对象
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 清除所有操作
/// </summary>
/// <returns></returns>
public string ClearAll()
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("drop user TestUser cascade");
//sqlstr.AppendLine("drop user TestUser cascade");//删除该用户的同时删除用户拥有的所有对象
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
return DateTime.Now.ToString();
}
catch (Exception e)
{
throw e;
}
}
///记录服务器IP
/// <summary>
/// 添加记录并记录数据库服务器IP
/// </summary>
/// <param name="rowid"></param>
/// <returns></returns>
public string TestIPAddTableData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("insert into TestUser.TestTable(testfield1,testfield2) values('" + rowid + "','测试2')");
string ipstr = "SELECT utl_inaddr.get_host_address FROM DUAL";
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
object obj = db.ExecuteScalar(CommandType.Text, ipstr);
return obj.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 更新记录
/// </summary>
/// <param name="rowid"></param>
public string TestIPUpdateData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("update TestUser.TestTable set testfield2='更改的测试2' where testfield1='" + rowid + "'");
string ipstr = "SELECT utl_inaddr.get_host_address FROM DUAL";
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
object obj = db.ExecuteScalar(CommandType.Text, ipstr);
return obj.ToString();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="rowid"></param>
public string TestIPDelTableData(string rowid)
{
Database db = DatabaseFactory.CreateDatabase(constr);
StringBuilder sqlstr = new StringBuilder();
sqlstr.AppendLine("delete TestUser.TestTable where testfield1='" + rowid + "'");
string ipstr = "SELECT utl_inaddr.get_host_address FROM DUAL";
try
{
db.ExecuteNonQuery(CommandType.Text, sqlstr.ToString());
object obj = db.ExecuteScalar(CommandType.Text, ipstr);
return obj.ToString();
}
catch (Exception e)
{
throw e;
}
}
}
调用页面上计算插入一万条数据记录的时间,页面后台代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
Label3.Text=TT.CreateDataUser();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
//Label1.Text = Convert.ToDateTime(Label3.Text).ToString()+"---"+Convert.ToDateTime(lstart.Text).ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
Label3.Text = TT.CreateTable();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button3_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
TT.AddTableData(i.ToString());
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button5_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
TT.UpdateData(i.ToString());
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button6_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
TT.DelTableData(i.ToString());
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button7_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
Label3.Text = TT.DelTable();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button8_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
Label3.Text = TT.DelDataUser();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button4_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
string d1 = TT.CreateDataUser();
string d2 = TT.CreateTable();
for (int i = 1; i <= 10000; i++)
{
TT.AddTableData(i.ToString());
}
for (int i = 1; i <= 10000; i++)
{
TT.UpdateData(i.ToString());
}
for (int i = 1; i <= 10000; i++)
{
TT.DelTableData(i.ToString());
}
string d4 = TT.DelTable();
Label3.Text = TT.DelDataUser();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
}
protected void Button9_Click(object sender, EventArgs e)
{
lstart.Text = DateTime.Now.ToString();
TestTime TT = new TestTime();
Label3.Text = TT.ClearAll();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
Label2.Text = "";
Label4.Text = "";
}
//求得两个日期之间的时间间隔
private double DateDiff(string howtocompare, DateTime date1, DateTime date2)
{
double diff = 0;
TimeSpan TS = new TimeSpan(date2.Ticks - date1.Ticks);
switch (howtocompare.ToLower())
{
case "year":
diff = Convert.ToDouble(TS.TotalDays / 365);
break;
case "month":
diff = Convert.ToDouble((TS.TotalDays / 365) * 12);
break;
case "day":
diff = Convert.ToDouble(TS.TotalDays);
break;
case "hour":
diff = Convert.ToDouble(TS.TotalHours);
break;
case "minute":
diff = Convert.ToDouble(TS.TotalMinutes);
break;
case "second":
diff = Convert.ToDouble(TS.TotalSeconds);
break;
}
return diff;
}
protected void Button10_Click(object sender, EventArgs e)
{
Label2.Text = "";
Label4.Text = "";
lstart.Text = DateTime.Now.ToString();
ArrayList item = new ArrayList();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
item.Add(TT.TestIPAddTableData(i.ToString()));
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
//附不同IP地址
//for (int i = 1; i < 5; i++)
//{
// item[i] = "172.15.142.1";
//}
for (int i = 0; i < 10000; i++)
{
Label2.Text += "-" + i.ToString() + "-----数据库IP地址:" + item[i].ToString() + "------------------<br/>";
}
StatiscIPServer(item);
}
private void StatiscIPServer(ArrayList item)
{
int k = item.Count;
for (int i = 0; i < k; i++)
{
if (item.Contains(item[i].ToString()))
{
if (IsExist(item[i].ToString(), staItem))
{
staItem.Add(item[i].ToString());
}
//item.RemoveAt(i);
// k = item.Count;
}
}
//显示不同IP的连接次数
for (int i = 0; i < staItem.Count; i++)
{
int n = 0;
for (int j = 0; j < item.Count; j++)
{
if (staItem[i].ToString() == item[j].ToString())//如果
{
n = n + 1;
}
}
Label4.Text += "----服务器IP地址为:" + staItem[i] + "的连接次数为:" + n + "次!<br/>";
}
}
private bool IsExist(string ipstr, ArrayList staItem)
{
if (staItem.Contains(ipstr))
return false;
else
return true;
}
protected void Button11_Click(object sender, EventArgs e)
{
Label2.Text = "";
Label4.Text = "";
ArrayList item = new ArrayList();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
item.Add(TT.TestIPUpdateData(i.ToString()));
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
附不同IP地址
//for (int i = 3; i < 5; i++)
//{
// item[i] = "172.15.142.1";
//}
for (int i = 0; i < 10000; i++)
{
Label2.Text += "-" + i.ToString() + "-----数据库IP地址:" + item[i].ToString() + "------------------<br/>";
}
StatiscIPServer(item);
}
protected void Button12_Click(object sender, EventArgs e)
{
Label2.Text = "";
Label4.Text = "";
ArrayList item = new ArrayList();
TestTime TT = new TestTime();
for (int i = 1; i <= 10000; i++)
{
item.Add(TT.TestIPDelTableData(i.ToString()));
}
Label3.Text = DateTime.Now.ToString();
Label1.Text = ((double)DateDiff("minute", Convert.ToDateTime(lstart.Text), Convert.ToDateTime(Label3.Text))).ToString();
附不同IP地址
//for (int i = 2; i < 10000 i=i+2)
//{
// item[i] = "172.15.142.1";
//}
for (int i = 0; i < 10000; i++)
{
Label2.Text += "-"+i.ToString()+"-----数据库IP地址:" + item[i].ToString() + "------------------<br/>";
}
StatiscIPServer(item);
}
}
前面控件如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
开始时间为:<asp:Label ID="lstart" runat="server"></asp:Label>
<br />
<br />
返回时间为:<asp:Label ID="Label3" runat="server"></asp:Label><br />
<br />
所花时间为:<asp:Label ID="Label1" runat="server"></asp:Label>分钟<br />
<br />
<asp:Button ID="Button1" runat="server" Text="创建用户账号" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="创建表" OnClick="Button2_Click" /> <asp:Button ID="Button3"
runat="server" Text="添加一万条数据" OnClick="Button3_Click" /> <asp:Button ID="Button5"
runat="server" OnClick="Button5_Click" Text="更新全部记录" />
<asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="删除全部记录" />
<asp:Button ID="Button7" runat="server" OnClick="Button7_Click" Text="删除表" />
<asp:Button ID="Button8" runat="server" OnClick="Button8_Click" Text="删除用户" /><br />
<br />
<asp:Button ID="Button9" runat="server" OnClick="Button9_Click" Text="清空所有操作" />
<asp:Button ID="Button4" runat="server" Text="计算全过程的时间" OnClick="Button4_Click" />
<br />
<br />
<br />
请先创建用户账号和创建表---------------------记录操作数据库的服务器IP和每个服务器的使用次数<br />
<asp:Button ID="Button10" runat="server" OnClick="Button10_Click" Text="插入数据" />
<asp:Button ID="Button11" runat="server" OnClick="Button11_Click" Text="更新数据" />
<asp:Button ID="Button12" runat="server" OnClick="Button12_Click" Text="删除数据" /><br />
<br />
<asp:Label ID="Label4" runat="server"></asp:Label><br />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</form>
</body>
</html>