这里用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();
}
}
}
结果:
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]