Asp.Net Oracle数据的通用操作类

在一个项目中,可能用到2种数据,比如说ORACLE ,MS_SQLSERVER 同时要用到,MS-SQL的数据通用类在很多的教程中都有现成的例子和代码,但是ORACLE比较少见,但是本次项目中正好用到,贴出来和大家一起共享。
这里用OraDbHelper.cs做常见的四种数据操作
一 ExecuteDataTable 返回到内存数据表
二 SqlDataReader 直接读数据
三 ExecuteScalar 获得一条数据
四 ExecuteNonQuery 对数据库执行增删改操作
环境:客户端WIN XP 安装的是ORACLE 8.17客户端
数据库服务器是:Red Hat Enterprise Linux AS 安装的是Oracle Database 10g
复制代码 代码如下:
SQL*Plus: Release 8.1.7.0.0 - Production on 星期六 12月 19 18:10:19 2009
(c) Copyright 2000 Oracle Corporation. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

首先 OraDbHelper.cs
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OracleClient;
namespace xjy.DAL
{
/// <summary>
/// OraDbHelper 对ORACLE 数据库操作的通用类
/// 作者;薛佳岳
/// 日期:2009-12-18
/// </summary>
public class OraDbHelper
{
private string connectionString;
/// <summary>
/// 设置数据库字符串的连接
/// </summary>
public string ConnectionString
{
set { connectionString = value; }
}
public OraDbHelper(string connectionString)
{
this.connectionString = connectionString;
}
#region ExecuteDataTable
/// <summary>
/// 执行一个查询,并返回结果集
/// </summary>
/// <param name="commandText">要执行的查询SQL文本命令</param>
/// <returns>返回查询结果集</returns>
public DataTable ExecuteDataTable(string commandText)
{
return ExecuteDataTable(commandText, CommandType.Text, null);
}
/// <summary>
/// 执行一个查询,并返回查询结果
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <returns>返回查询结果集</returns>
public DataTable ExecuteDataTable(string commandText, CommandType commandType)
{
return ExecuteDataTable(commandText, commandType, null);
}
/// <summary>
/// 执行一个查询,并返回查询结果
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <param name="parameters">PL-SQL 语句或存储过程的参数数组</param>
/// <returns></returns>
public DataTable ExecuteDataTable(string commandText, CommandType commandType, params OracleParameter[] parameters)
{
DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand command = new OracleCommand(commandText, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
//通过包含查询SQL的SqlCommand实例来实例化SqlDataAdapter
OracleDataAdapter adapter = new OracleDataAdapter(command);
adapter.Fill(data);//填充DataTable
}
}
return data;
}
#endregion ExecuteDataTable
#region ExecuteReader
/// <summary>
/// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。
/// </summary>
/// <param name="commandText">要执行的查询SQL文本命令</param>
/// <returns></returns>
public OracleDataReader ExecuteReader(string commandText)
{
return ExecuteReader(commandText, CommandType.Text, null);
}
/// <summary>
/// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <returns></returns>
public OracleDataReader ExecuteReader(string commandText, CommandType commandType)
{
return ExecuteReader(commandText, commandType, null);
}
/// <summary>
/// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param>
/// <returns></returns>
public OracleDataReader ExecuteReader(string commandText, CommandType commandType, OracleCommand[] parameters)
{
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleCommand parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();
//CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion ExecuteReader
#region ExecuteScalar
/// <summary>
/// 从数据库中检索单个值(例如一个聚合值)。
/// </summary>
/// <param name="commandText">要执行的查询PL-SQL文本命令</param>
/// <returns></returns>
public Object ExecuteScalar(string commandText)
{
return ExecuteScalar(commandText, CommandType.Text, null);
}
/// <summary>
/// 从数据库中检索单个值(例如一个聚合值)。
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者PL-SQL文本命令</param>
/// <returns></returns>
public Object ExecuteScalar(string commandText, CommandType commandType)
{
return ExecuteScalar(commandText, commandType, null);
}
/// <summary>
/// 从数据库中检索单个值(例如一个聚合值)。
/// </summary>
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <param name="parameters">PL-SQL 语句或存储过程的参数数组</param>
/// <returns></returns>
public Object ExecuteScalar(string commandText, CommandType commandType, OracleParameter[] parameters)
{
object result = null;
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand command = new OracleCommand(commandText, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
result = command.ExecuteScalar();
}
}
return result;//返回查询结果的第一行第一列,忽略其它行和列
}
#endregion ExecuteScalar
#region ExecuteNonQuery
/// <summary>
/// 对数据库执行增删改操作
/// </summary>
/// <param name="commandText">要执行的查询pl-sql文本命令</param>
/// <returns></returns>
public int ExecuteNonQuery(string commandText)
{
return ExecuteNonQuery(commandText, CommandType.Text, null);
}
/// <summary>
/// 对数据库执行增删改操作
/// </summary>
/// <param name="commandText">要执行的pl-sql语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者pl-sql文本命令</param>
/// <returns></returns>
public int ExecuteNonQuery(string commandText, CommandType commandType)
{
return ExecuteNonQuery(commandText, commandType, null);
}
/// <summary>
/// 对数据库执行增删改操作
/// </summary>
/// <param name="commandText">要执行的pl-sql语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者pl-sql文本命令</param>
/// <param name="parameters">pl-sql 语句或存储过程的参数数组</param>
/// <returns>返回执行操作受影响的行数</returns>
public int ExecuteNonQuery(string commandText, CommandType commandType, OracleParameter[] parameters)
{
int count = 0;
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand command = new OracleCommand(commandText, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
count = command.ExecuteNonQuery();
}
}
return count;//返回执行增删改操作之后,数据库中受影响的行数
}
#endregion ExecuteNonQuery
}
}

一测试datatable
前台代码
复制代码 代码如下:
<%@ 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">
<div>
<ASP:GridView ID="GridView1" runat="server">
</ASP:GridView>
</div>
</form>
</body>
</html>

后台代码
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xjy.DAL;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConnectionStringSettings setting = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"];
string myStr = setting.ConnectionString;
OraDbHelper myora = new OraDbHelper(myStr);
DataTable t1 = new DataTable();
t1=myora.ExecuteDataTable("select art_no,descr from article where art_no<100");
GridView1.DataSource=t1;
GridView1.DataBind();
}
}

结果如图


二 测试 SqlDataReader
前台代码不变
后台代码如下:
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xjy.DAL;
using System.Data.SqlClient;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConnectionStringSettings setting = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"];
string myStr = setting.ConnectionString;
OraDbHelper myora = new OraDbHelper(myStr);
OracleDataReader myDr = myora.ExecuteReader("select art_no,descr from article where art_no<100");
while (myDr.Read())
{
Response.Write("|" + myDr[0] + "|" + myDr[1]+"
");
Response.Write("____________________________________");
}
myDr.Close();
myDr.Dispose();
}
}

结果如图

三测试ExecuteScalar
前台代码
复制代码 代码如下:
<%@ 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">
<div>
<ASP:Label ID="Label1" runat="server" Text="Label"></ASP:Label>
</div>
</form>
</body>
</html>

后台代码
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xjy.DAL;
using System.Data.SqlClient;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConnectionStringSettings setting = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"];
string myStr = setting.ConnectionString;
OraDbHelper myora = new OraDbHelper(myStr);
string shuliang;
shuliang = myora.ExecuteScalar("select count(art_no) from article where art_no<100").ToString();
Label1.Text = shuliang;
}
}

结果如图:

四测试ExecuteNonQuery

前台代码:
复制代码 代码如下:
<%@ 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">
<div> <ASP:Label ID="Label1" runat="server" Text="Label"></ASP:Label>
<ASP:GridView ID="GridView1" runat="server">
</ASP:GridView>
<ASP:Label ID="Label2" runat="server" Text="Label"></ASP:Label>
<ASP:GridView ID="GridView2" runat="server">
</ASP:GridView>
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="恢复值" />
</div>
</form>
</body>
</html>

后台代码
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xjy.DAL;
using System.Data.SqlClient;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ConnectionStringSettings setting = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"];
string myStr = setting.ConnectionString;
OraDbHelper myora = new OraDbHelper(myStr);
DataTable t1 = new DataTable();
Label1.Text = "更新前的值";
t1 = myora.ExecuteDataTable("select art_no,descr from article where art_no=8");
GridView1.DataSource = t1;
GridView1.DataBind();
if (myora.ExecuteNonQuery("update article set descr='更新描述测试值' where art_no=8")>0)
{
Label2.Text = "更新后的值";
t1 = myora.ExecuteDataTable("select art_no,descr from article where art_no=8");
GridView2.DataSource = t1;
GridView2.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ConnectionStringSettings setting = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"];
string myStr = setting.ConnectionString;
OraDbHelper myora = new OraDbHelper(myStr);
DataTable t1 = new DataTable();
if (myora.ExecuteNonQuery("update article set descr='可怜可怜60ML/瓶' where art_no=8") > 0)
{
Label2.Text = "更新后的值";
t1 = myora.ExecuteDataTable("select art_no,descr from article where art_no=8");
GridView2.DataSource = t1;
GridView2.DataBind();
}
}
}

结果:

AspNet技术Asp.Net Oracle数据的通用操作类,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。