OracleHelper[.Net 连接Oracle数据库的封装类]
using System;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Data.OleDb; namespace DBUtility
{ /// <summary>
/// A helper class used to execute queries against an Oracle database
/// </summary>
public abstract class OracleHelper
{ // Read the connection strings from the configuration file
public static readonly string connectionString = ConfigurationManager.AppSettings["OrdersDAL"]; //Create a hashtable for the parameter cached
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// <summary>
/// Execute a database query which does not include a select
/// </summary>
/// <param name="connString">Connection string to database</param>
/// <param name="cmdType">Command type either stored procedure or SQL</param>
/// <param name="cmdText">Acutall SQL Command</param>
/// <param name="commandParameters">Parameters to bind to the command</param>
/// <returns></returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
// Create a new Oracle command
OleDbCommand cmd = new OleDbCommand(); //Create a connection
using (OleDbConnection connection = new OleDbConnection(connectionString))
{ //Prepare the command
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); //Execute the command
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// Execute an OleDbCommand (that returns no resultset) against an existing database transaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter(":prodid", 24));
/// </remarks>
/// <param name="trans">an existing database transaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// Execute an OleDbCommand (that returns no resultset) against an existing database connection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter(":prodid", 24));
/// </remarks>
/// <param name="conn">an existing database connection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(OleDbConnection connection, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{ OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// Execute a select query that will return a result set
/// </summary>
/// <param name="connString">Connection string</param>
//// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{ //Create the command and connection
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(connectionString); try
{
//Prepare the command to execute
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); //Execute the query, stating that the connection should close when the resulting datareader has been read
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr; }
catch
{ //If an error occurs close the connection as the reader will not be used and we expect it to close the connection
conn.Close();
throw;
}
} /// <summary>
/// Execute an OleDbCommand that returns the first column of the first record against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter(":prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand(); using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// Execute a OleDbCommand (that returns a 1x1 resultset) against the specified SqlTransaction
/// using the provided parameters.
/// </summary>
/// <param name="transaction">A valid SqlTransaction</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">An array of OracleParamters used to execute the command</param>
/// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
if (transaction == null)
throw new ArgumentNullException("transaction");
if (transaction != null && transaction.Connection == null)
throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); // Create a command and prepare it for execution
OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters); // Execute the command & return the results
object retval = cmd.ExecuteScalar(); // Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
} /// <summary>
/// Execute an OleDbCommand that returns the first column of the first record against an existing database connection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(conn, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter(":prodid", 24));
/// </remarks>
/// <param name="conn">an existing database connection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
public static object ExecuteScalar(OleDbConnection connectionString, CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand(); PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// Add a set of parameters to the cached
/// </summary>
/// <param name="cacheKey">Key value to look up the parameters</param>
/// <param name="commandParameters">Actual parameters to cached</param>
public static void CacheParameters(string cacheKey, params OleDbParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
} /// <summary>
/// Fetch parameters from the cache
/// </summary>
/// <param name="cacheKey">Key to look up the parameters</param>
/// <returns></returns>
public static OleDbParameter [] GetCachedParameters(string cacheKey)
{
OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey]; if (cachedParms == null)
return null; // If the parameters are in the cache
OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length]; // return a copy of the parameters
for (int i = , j = cachedParms.Length; i < j; i++)
clonedParms[i] = (OleDbParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms;
} /// <summary>
/// Internal function to prepare a command for execution by the database
/// </summary>
/// <param name="cmd">Existing command object</param>
/// <param name="conn">Database connection object</param>
/// <param name="trans">Optional transaction object</param>
/// <param name="cmdType">Command type, e.g. stored procedure</param>
/// <param name="cmdText">Command test</param>
/// <param name="commandParameters">Parameters for the command</param>
private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] commandParameters)
{ //Open the connection if required
if (conn.State != ConnectionState.Open)
conn.Open(); //Set up the command
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType; //Bind it to the transaction if it exists
if (trans != null)
cmd.Transaction = trans; // Bind the parameters passed in
if (commandParameters != null)
{
foreach (OleDbParameter parm in commandParameters)
cmd.Parameters.Add(parm);
}
} /// <summary>
/// Converter to use boolean data type with Oracle
/// </summary>
/// <param name="value">Value to convert</param>
/// <returns></returns>
public static string OraBit(bool value)
{
if (value)
return "Y";
else
return "N";
} /// <summary>
/// Converter to use boolean data type with Oracle
/// </summary>
/// <param name="value">Value to convert</param>
/// <returns></returns>
public static bool OraBool(string value)
{
if (value.Equals("Y"))
return true;
else
return false;
}
}
}
OracleHelper[.Net 连接Oracle数据库的封装类]的更多相关文章
- C# 连接 Oracle数据库增删改查,事务
一. 前情提要 一般.NET环境连接Oracle数据库,是通过 TNS/SQL.NET 配置文件,而 TNS 必须要 Oracle 客户端(如果连接的是服务器的数据库,本地还要装一个 client , ...
- java连接Oracle数据库
Oracle数据库先创建一个表和添加一些数据 1.先在Oracle数据库中创建一个student表: create table student ( id ) not null primary key, ...
- 记录排查解决Hubble.Net连接Oracle数据库建立镜像库数据丢失的问题
起因 前几天在弄Hubble连接Oracle数据库,然后在mongodb中建立一个镜像数据库; 发现一个问题,原本数据是11W,但是镜像库中只有6w多条; 刚开始以为是没运行好,又rebuild了一下 ...
- NodeJs连接Oracle数据库
nodejs连接oracle数据库,各个平台的官方详情文档:https://github.com/oracle/node-oracledb/blob/master/INSTALL.md 我的nodej ...
- jdbc连接oracle数据库
/*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...
- 用VS连接oracle数据库时ORA-12504错误
在用VS2008连接oracle数据库时,可能会出现: ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME 只需在web.config文件Data ...
- ASP.NET连接Oracle数据库的步骤详解(转)
ASP.NET连接Oracle数据库的步骤详解 本文我们主要介绍了ASP.NET连接Oracle数据库的步骤及每个步骤需要进行的设置,希望能够对您有所帮助. 在用ASP.NET开发应用程序时, ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- [原创]安装Oracle 11gR2,以及如何在win8下使用plsql develper连接Oracle数据库 ,在这里和大家分享下
一,关于win8下安装Oracle 11gR2 1.我下载的是Oracle_11gR2_win64.其中有两个包: 注意:在解压了之后将:win64_11gR2_database_2of2\datab ...
随机推荐
- Js编码和Java后台解码
1.java.将resultMsg 转为utf-8 (1) resultMsg = URLEncoder.encode(resultMsg, "utf-8"); (2) new S ...
- Codevs 1048 石子归并
1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合 ...
- PHP 易出问题记录
PHP foreach引用缺陷 <?php $array = array(1, 2, 3); foreach ($array as &$v) {} foreach ($array as ...
- C语言结构体中的函数指针
这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...
- sublimeformaya
网上没有找到这样的插件自己造了一个 https://github.com/jonntd/connectionmaya 附件列表
- android 布局权重问题(转载)
//权重和父容器orientation有关 horizontal 指水平方向权重 android:layout_width vertical 指垂直方向权重 android:layout_he ...
- 如何利用SecureCRT连接Ubuntu12.0.4
环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...
- C# 使用Salt+Hash来为密码加密
(一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到 ...
- 【Python网络爬虫三】 爬去网页新闻
学弟又一个自然语言处理的项目,需要在网上爬一些文章,然后进行分词,刚好牛客这周的是从一个html中找到正文,就实践了一下.写了一个爬门户网站新闻的程序 需求: 从门户网站爬取新闻,将新闻标题,作者,时 ...
- Apple 公司开发者账号注册
苹果公司开发者账号注册流程详解 这段时间在给朋友申请苹果账号,从个人开发者账号.公司账号到企业账号,申请了个遍.这里对申请流程做一下介绍,方便其他朋友,少走弯路,账号早日申请通过. 1.首先介绍下 ...