c#操作oracle的通用类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DDTek.Oracle;
using System.Configuration;
namespace Common
{
//// <summary>
/// OracleHelper 的摘要说明。
/// </summary>
public class OracleHelper
{
protected OracleConnection Connection;
private string connectionString;
public OracleHelper()
{
string connStr;
connStr = System.Configuration.ConfigurationSettings.AppSettings["connStr"].ToString();
connectionString = connStr;
Connection = new OracleConnection(connectionString);
}
#region 带参数的构造函数
/**/
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="ConnString">数据库联接字符串</param>
public OracleHelper(string ConnString)
{
string connStr;
connStr = System.Configuration.ConfigurationSettings.AppSettings[ConnString].ToString();
Connection = new OracleConnection(connStr);
}
#endregion
#region 打开数据库
/**/
/// <summary>
/// 打开数据库
/// </summary>
public void OpenConn()
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
}
#endregion
#region 关闭数据库联接
/**/
/// <summary>
/// 关闭数据库联接
/// </summary>
public void CloseConn()
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
#endregion
#region 执行SQL语句,返回数据到DataSet中
/**/
/// <summary>
/// 执行SQL语句,返回数据到DataSet中
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="DataSetName">自定义返回的DataSet表名</param>
/// <returns>返回DataSet</returns>
public DataSet ReturnDataSet(string sql, string DataSetName)
{
DataSet dataSet = new DataSet();
OpenConn();
OracleDataAdapter OraDA = new OracleDataAdapter(sql, Connection);
OraDA.Fill(dataSet, DataSetName);
//CloseConn();
return dataSet;
}
#endregion
#region 执行Sql语句,返回带分页功能的dataset
/**/
/// <summary>
/// 执行Sql语句,返回带分页功能的dataset
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="PageSize">每页显示记录数</param>
/// <param name="CurrPageIndex"><当前页/param>
/// <param name="DataSetName">返回dataset表名</param>
/// <returns>返回DataSet</returns>
public DataSet ReturnDataSet(string sql, int PageSize, int CurrPageIndex, string DataSetName)
{
DataSet dataSet = new DataSet();
OpenConn();
OracleDataAdapter OraDA = new OracleDataAdapter(sql, Connection);
OraDA.Fill(dataSet, PageSize * (CurrPageIndex - 1), PageSize, DataSetName);
//CloseConn();
return dataSet;
}
#endregion
#region 执行SQL语句,返回 DataReader,用之前一定要先.read()打开,然后才能读到数据
/**/
/// <summary>
/// 执行SQL语句,返回 DataReader,用之前一定要先.read()打开,然后才能读到数据
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>返回一个OracleDataReader</returns>
public OracleDataReader ReturnDataReader(String sql)
{
OpenConn();
OracleCommand command = new OracleCommand(sql, Connection);
return command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
#endregion
#region 执行SQL语句,返回记录总数数
/**/
/// <summary>
/// 执行SQL语句,返回记录总数数
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns>返回记录总条数</returns>
public int GetRecordCount(string sql)
{
int recordCount = 0;
OpenConn();
OracleCommand command = new OracleCommand(sql, Connection);
OracleDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
recordCount++;
}
dataReader.Close();
//CloseConn();
return recordCount;
}
#endregion
#region 取当前序列,条件为seq.nextval或seq.currval
/**/
/// <summary>
/// 取当前序列
/// </summary>
/// <param name="seqstr"></param>
/// <param name="table"></param>
/// <returns></returns>
public decimal GetSeq(string seqstr)
{
decimal seqnum = 0;
string sql = "select " + seqstr + " from dual";
OpenConn();
OracleCommand command = new OracleCommand(sql, Connection);
OracleDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
seqnum = decimal.Parse(dataReader[0].ToString());
}
dataReader.Close();
// CloseConn();
return seqnum;
}
#endregion
#region 执行SQL语句,返回所影响的行数
/**/
/// <summary>
/// 执行SQL语句,返回所影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteSQL(string sql)
{
int Cmd = 0;
OpenConn();
OracleCommand command = new OracleCommand(sql, Connection);
try
{
Cmd = command.ExecuteNonQuery();
}
catch
{
}
finally
{
// CloseConn();
}
return Cmd;
}
#endregion
// ===========================================
// ==用hashTable对数据库进行insert,update,del操作,注意此时只能用默认的数据库连接"connstr"==
// ============================================
#region 根据表名及哈稀表自动插入数据库 用法:Insert("test",ht)
public int Insert(string TableName, Hashtable ht)
{
OracleParameter[] Parms = new OracleParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
DataTable dt = GetTabType(TableName);
OracleDbType otype;
int size = 0;
int i = 0;
while (et.MoveNext()) // 作哈希表循环
{
GetoType(et.Key.ToString().ToUpper(), dt, out otype, out size);
OracleParameter op = MakeParam(":" + et.Key.ToString(), otype, size, et.Value.ToString());
Parms[i] = op; // 添加SqlParameter对象
i = i + 1;
}
string str_Sql = GetInsertSqlbyHt(TableName, ht); // 获得插入sql语句
int val = ExecuteNonQuery(str_Sql, Parms);
return val;
}
#endregion
#region 根据相关条件对数据库进行更新操作 用法:Update("test","Id=:Id",ht);
public int Update(string TableName, string ht_Where, Hashtable ht)
{
OracleParameter[] Parms = new OracleParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
DataTable dt = GetTabType(TableName);
OracleDbType otype;
int size = 0;
int i = 0;
// 作哈希表循环
while (et.MoveNext())
{
GetoType(et.Key.ToString().ToUpper(), dt, out otype, out size);
OracleParameter op = MakeParam(":" + et.Key.ToString(), otype, size, et.Value.ToString());
Parms[i] = op; // 添加SqlParameter对象
i = i + 1;
}
string str_Sql = GetUpdateSqlbyHt(TableName, ht_Where, ht); // 获得插入sql语句
int val = ExecuteNonQuery(str_Sql, Parms);
return val;
}
public int Update(string TableName, string ht_Where, Hashtable ht,OracleConnection oraConn)
{
OracleParameter[] Parms = new OracleParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
DataTable dt = GetTabType(TableName);
OracleDbType otype;
int size = 0;
int i = 0;
// 作哈希表循环
while (et.MoveNext())
{
GetoType(et.Key.ToString().ToUpper(), dt, out otype, out size);
OracleParameter op = MakeParam(":" + et.Key.ToString(), otype, size, et.Value.ToString());
Parms[i] = op; // 添加SqlParameter对象
i = i + 1;
}
string str_Sql = GetUpdateSqlbyHt(TableName, ht_Where, ht); // 获得插入sql语句
int val = ExecuteNonQuery(str_Sql, oraConn, Parms);
return val;
}
#endregion
#region del操作,注意此处条件个数与hash里参数个数应该一致 用法:Del("test","Id=:Id",ht)
public int Del(string TableName, string ht_Where, Hashtable ht)
{
OracleParameter[] Parms = new OracleParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
DataTable dt = GetTabType(TableName);
OracleDbType otype;
int i = 0;
int size = 0;
// 作哈希表循环
while (et.MoveNext())
{
GetoType(et.Key.ToString().ToUpper(), dt, out otype, out size);
OracleParameter op = MakeParam(":" + et.Key.ToString(), et.Value.ToString());
Parms[i] = op; // 添加SqlParameter对象
i = i + 1;
}
string str_Sql = GetDelSqlbyHt(TableName, ht_Where, ht); // 获得删除sql语句
int val = ExecuteNonQuery(str_Sql, Parms);
return val;
}
#endregion
// ===========================================
// ========上面三个操作的内部调用函数==================
// ===========================================
#region 根据哈稀表及表名自动生成相应insert语句(参数类型的)
/**/
/// <summary>
/// 根据哈稀表及表名自动生成相应insert语句
/// </summary>
/// <param name="TableName">要插入的表名</param>
/// <param name="ht">哈稀表</param>
/// <returns>返回sql语句</returns>
public static string GetInsertSqlbyHt(string TableName, Hashtable ht)
{
string str_Sql = "";
int i = 0;
int ht_Count = ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string before = "";
string behide = "";
while (myEnumerator.MoveNext())
{
if (i == 0)
{
before = "(" + myEnumerator.Key;
}
else if (i + 1 == ht_Count)
{
before = before + "," + myEnumerator.Key + ")";
}
else
{
before = before + "," + myEnumerator.Key;
}
i = i + 1;
}
behide = " Values" + before.Replace(",", ",:").Replace("(", "(:");
str_Sql = "Insert into " + TableName + before + behide;
return str_Sql;
}
#endregion
#region 根据表名,where条件,哈稀表自动生成更新语句(参数类型的)
public static string GetUpdateSqlbyHt(string Table, string ht_Where, Hashtable ht)
{
string str_Sql = "";
int i = 0;
int ht_Count = ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while (myEnumerator.MoveNext())
{
if (i == 0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key + "=:" + myEnumerator.Key).ToLower()) == -1)
{
str_Sql = myEnumerator.Key + "=:" + myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf((":" + myEnumerator.Key + " ").ToLower()) == -1)
{
str_Sql = str_Sql + "," + myEnumerator.Key + "=:" + myEnumerator.Key;
}
}
i = i + 1;
}
if (ht_Where == null || ht_Where.Replace(" ", "") == "") // 更新时候没有条件
{
str_Sql = "update " + Table + " set " + str_Sql;
}
else
{
str_Sql = "update " + Table + " set " + str_Sql + " where " + ht_Where;
}
str_Sql = str_Sql.Replace("set ,", "set ").Replace("update ,", "update ");
return str_Sql;
}
#endregion
#region 根据表名,where条件,哈稀表自动生成del语句(参数类型的)
public static string GetDelSqlbyHt(string Table, string ht_Where, Hashtable ht)
{
string str_Sql = "";
int i = 0;
int ht_Count = ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while (myEnumerator.MoveNext())
{
if (i == 0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key + "=:" + myEnumerator.Key).ToLower()) == -1)
{
str_Sql = myEnumerator.Key + "=:" + myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf((":" + myEnumerator.Key + " ").ToLower()) == -1)
{
str_Sql = str_Sql + "," + myEnumerator.Key + "=:" + myEnumerator.Key;
}
}
i = i + 1;
}
if (ht_Where == null || ht_Where.Replace(" ", "") == "") // 更新时候没有条件
{
str_Sql = "Delete " + Table;
}
else
{
str_Sql = "Delete " + Table + " where " + ht_Where;
}
return str_Sql;
}
#endregion
#region 生成oracle参数
/**/
/// <summary>
/// 生成oracle参数
/// </summary>
/// <param name="ParamName">字段名</param>
/// <param name="otype">数据类型</param>
/// <param name="size">数据大小</param>
/// <param name="Value">值</param>
/// <returns></returns>
public static OracleParameter MakeParam(string ParamName, OracleDbType otype, int size, Object Value)
{
OracleParameter para = new OracleParameter(ParamName, Value);
para.OracleDbType = otype;
para.Size = size;
return para;
}
#endregion
#region 生成oracle参数
public static OracleParameter MakeParam(string ParamName, string Value)
{
return new OracleParameter(ParamName, Value);
}
#endregion
#region 根据表结构字段的类型和长度拼装oracle sql语句参数
public static void GetoType(string key, DataTable dt, out OracleDbType otype, out int size)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "column_name='" + key + "'";
string fType = dv[0]["data_type"].ToString().ToUpper();
switch (fType)
{
case "DATE":
otype = OracleDbType.Timestamp;
size = int.Parse(dv[0]["data_length"].ToString());
break;
case "CHAR":
otype = OracleDbType.Char;
size = int.Parse(dv[0]["data_length"].ToString());
break;
case "LONG":
otype = OracleDbType.Double;
size = int.Parse(dv[0]["data_length"].ToString());
break;
case "NVARCHAR2":
otype = OracleDbType.NVarChar;
size = int.Parse(dv[0]["data_length"].ToString());
break;
case "VARCHAR2":
otype = OracleDbType.NVarChar;
size = int.Parse(dv[0]["data_length"].ToString());
break;
default:
otype = OracleDbType.NVarChar;
size = 100;
break;
}
}
#endregion
#region 动态 取表里字段的类型和长度,此处没有动态用到connstr,是默认的!by/文少
public System.Data.DataTable GetTabType(string tabnale)
{
string sql = "select column_name,data_type,data_length from all_tab_columns where table_name='" + tabnale.ToUpper() + "'";
OpenConn();
return (ReturnDataSet(sql, "dv")).Tables[0];
}
#endregion
#region 执行sql语句
public int ExecuteNonQuery(string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OpenConn();
cmd.Connection = Connection;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
// conn.CloseConn();
return val;
}
public int ExecuteNonQuery(string cmdText, OracleConnection oraConn, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = oraConn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
// conn.CloseConn();
return val;
}
#endregion
// ========================================
// =========内部调用函数完=======================
// ========================================
}
}
c#操作oracle的通用类的更多相关文章
- php操作oracle的方法类集全
在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...
- 一个操作oracle的c#类 含分页
有别于以前的一个OracleHelper,这个版各有所长,MARK下. using System; using System.Data; using System.Data.OracleClient; ...
- C#操作SQL Server通用类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- java操作数据库的通用的类
package cn.dao; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; ...
- 使用C# 操作存储过程,执行sql语句通用类
如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码: /// <summary> /// Sql通用类 /// </summary> ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .
//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...
- C#.NET操作数据库通用类
下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句.执行存储过程.以下是其详细实现过程,希望大家共同修改优化之.稍后将介绍如何使用它 ...
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库
前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...
随机推荐
- 4.请求方式为application/json时的接口测试要如何做?
1.单独新建一个线程组, 在信息头管理器中增加: 在Body Data中,添加json格式的内容(我不会写,直接用fiddler抓包,然后把TextView中的数据复制过来). 2.然后因为登录状态, ...
- scala调用外部命令
scala调用外部命令 1. 导入sys.process包 2. 调用方式:" 外部命令 " ! 双引号内+外部命令+感叹号 例: scala调用外部命令工作原 ...
- 移动app的一些心得
周末的时候闲着没事,看到了个开源的红旅动漫的APP,于是自己就拿下来写了下,整个网站作者也解析成了接口,所以就可以写成app了,作者写的是用mui开发的,可以通过hbuilder软件打开,进行查看源代 ...
- hexo常用命令笔记
hexo npm install -g hexo npm update -g hexo hexo init 常用 hexo n == hexo new "a new post" 新 ...
- Javascript获取不重复的随机数值
/** * 获取不重复随机数 * @param integer start 随机数最小值 * @param integer end 随机数最大值 * @param integer size 随机数获取 ...
- 获取radio的值
随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox.Radiobutton .DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的操作 ...
- LightOJ 1337 F - The Crystal Maze (bfs)
Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...
- python 函数/列表的应用
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a1 b2 c ...
- erlang四大behaviour之三-gen_event
来源:http://www.cnblogs.com/puputu/articles/1689623.html 1. 事件处理规则 在OTP中,事件管理器是一个事件可以发送到的命名对象,一个事件可以是一 ...
- XueTr 0.45 (手工杀毒辅助工具) 绿色版
软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...