Oracle 数据库操作类

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Transactions;
/// <summary>
/// DB 的摘要说明 Written By Luos.Luo ,the creator of SalePlayer.Com
/// </summary>
public class MyOraDB
{
public MyOraDB()
{
}
public int ExcuteSqlWithNoQuery(string vSql)
{
int vI = 0;
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleCommand vOracleCmd = new OracleCommand();
vOracleCmd.Connection = vOracleConn;
vOracleCmd.CommandText = vSql;
vOracleCmd.CommandType = CommandType.Text;
vI = vOracleCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("MyOraDB", vSql, ex);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vI;
}
public int ExcuteSqlWithSingleNum(string vSql)
{
int vI = 0;
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleDataReader vOracleDataReader = CreateOracleDataReader(vSql);
while (vOracleDataReader.Read())
{
vI = vOracleDataReader.GetInt32(0);
}
vOracleDataReader.Close();
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("MyOraDB", vSql, ex);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vI;
}
public string ExcuteSqlWithSingleString(string vSql)
{
StringBuilder vTempStrBld = new StringBuilder();
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleDataReader vOracleDataReader = CreateOracleDataReader(vSql);
while (vOracleDataReader.Read())
{
vTempStrBld.Append(vOracleDataReader.GetString(0));
}
vOracleDataReader.Close();
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("MyOraDB", vSql, ex);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vTempStrBld.ToString();
}
public DataTable ExcuteSqlWithDataTable(string vSql)
{
DataTable vDataTable = new DataTable();
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleDataAdapter vOracleDataAdapter = new OracleDataAdapter(vSql, vOracleConn);
vOracleDataAdapter.Fill(vDataTable);
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("MyOraDB", vSql, ex);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vDataTable;
}
public DataSet ExcuteSqlWithDataSet(string vSql)
{
DataSet vDataSet = new DataSet();
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleDataAdapter vOracleDataAdapter = new OracleDataAdapter(vSql, vOracleConn);
vOracleDataAdapter.Fill(vDataSet);
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("MyOraDB", vSql, ex);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vDataSet;
}
public string ExcuteSqlTransactionWithString(string[] vSqlArray)
{
int vI = vSqlArray.Length;
string vSql = string.Empty;
OracleConnection vOracleConn = OpenOracleDBConn();
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleTransaction vOracleTrans = vOracleConn.BeginTransaction(IsolationLevel.ReadCommitted);
OracleCommand vOracleCmd = new OracleCommand();
vOracleCmd.Connection = vOracleConn;
vOracleCmd.Transaction = vOracleTrans;
try
{
for (int i = 0; i < vI; i++)
{
if (string.IsNullOrEmpty(vSqlArray[i]) == false)
{
vSql = vSqlArray[i];
vOracleCmd.CommandText = vSql;
vOracleCmd.ExecuteNonQuery();
}
}
vOracleTrans.Commit();
}
catch (Exception ex)
{
vOracleTrans.Rollback();
CloseOracleDBConn(vOracleConn);
MyLog vMyLog = new MyLog();
vMyLog.WriteLog("", vSql, ex);
return ex.Message;
}
CloseOracleDBConn(vOracleConn);
return "SUCCESS";
}
public void ExcuteProcedureWithNonQuery(string vProcedureName, OracleParameter[] parameters)
{
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleCommand vOracleCmd = new OracleCommand();
vOracleCmd.Connection = vOracleConn;
vOracleCmd.CommandText = vProcedureName;
vOracleCmd.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
vOracleCmd.Parameters.Add(parameter);
}
vOracleCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
WriteLog("异常信息:ExcuteProcedureWithNonQuery:" + ex.Message);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
}
public string ExcuteProcedureWithSingleString(string vProcedureName, OracleParameter[] parameters)
{
string vTempStr = string.Empty;
OracleParameter vOutMessage;
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleCommand vOracleCmd = new OracleCommand();
vOracleCmd.Connection = vOracleConn;
vOracleCmd.CommandText = vProcedureName;
vOracleCmd.CommandType = CommandType.StoredProcedure;
vOutMessage = new OracleParameter("O_FLAG", OracleType.VarChar);
vOutMessage.Direction = ParameterDirection.Output;
vOutMessage.Size = 100;
vOracleCmd.Parameters.Add(vOutMessage);
foreach (OracleParameter parameter in parameters)
{
vOracleCmd.Parameters.Add(parameter);
}
vOracleCmd.ExecuteNonQuery();
vOracleCmd.Dispose();
vOracleCmd = null;
vTempStr = vOutMessage.Value.ToString();
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
WriteLog("异常信息:ExcuteProcedureWithNonQuery:" + ex.Message);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vTempStr;
}
public int ExcuteProcedureWithSingleNum(string vProcedureName, OracleParameter[] parameters)
{
int vI = 0;
OracleParameter vOutMessage;
OracleConnection vOracleConn = OpenOracleDBConn();
try
{
if (vOracleConn.State != ConnectionState.Open)
{
vOracleConn.Open();
}
OracleCommand vOracleCmd = new OracleCommand();
vOracleCmd.Connection = vOracleConn;
vOracleCmd.CommandText = vProcedureName;
vOracleCmd.CommandType = CommandType.StoredProcedure;
vOutMessage = new OracleParameter("O_FLAG", OracleType.Int32);
vOutMessage.Direction = ParameterDirection.Output;
vOutMessage.Size = 100;
vOracleCmd.Parameters.Add(vOutMessage);
foreach (OracleParameter parameter in parameters)
{
vOracleCmd.Parameters.Add(parameter);
}
vOracleCmd.ExecuteNonQuery();
vOracleCmd.Dispose();
vOracleCmd = null;
vI = System.Convert.ToInt32(vOutMessage.Value);
}
catch (Exception ex)
{
MyLog vMyLog = new MyLog();
WriteLog("异常信息:ExcuteProcedureWithNonQuery:" + ex.Message);
}
finally
{
CloseOracleDBConn(vOracleConn);
}
return vI;
}
/// <summary>
/// Creates the parameter.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="dbType">Type of the db.</param>
/// <param name="size">The value size</param>
/// <param name="direction">The direction.</param>
/// <param name="paramValue">The param value.</param>
/// <returns></returns>
public OracleParameter CreateParameter(string vProcedureName, OracleType vOracleType, int vSize, ParameterDirection vDirection, object vParamValue)
{
OracleParameter vOracleParameter = new OracleParameter();
vOracleParameter.ParameterName = vProcedureName;
vOracleParameter.OracleType = vOracleType;
vOracleParameter.Size = vSize;
vOracleParameter.Direction = vDirection;
if (!(vOracleParameter.Direction == ParameterDirection.Output))
{
vOracleParameter.Value = vParamValue;
}
return vOracleParameter;
}
private OracleConnection OpenOracleDBConn()
{
string vConnStr = string.Empty;
string vOraDBName = System.Configuration.ConfigurationManager.AppSettings["OraDBName"];
switch (vOraDBName)
{
case "MESDB_03":
vConnStr = "Data Source=SZMESDB;Persist Security Info=True;User ID=MESDB_03;Password=MESDB;Unicode=True;";
break;
case "MESDBTEST_03":
vConnStr = "Data Source=SZMESDB;Persist Security Info=True;User ID=MESDB_03;Password=MESDB;Unicode=True;";
break;
default:
vConnStr = "Data Source=SZMESDB;Persist Security Info=True;User ID=MESDBTEST_03;Password=MESDB;Unicode=True;";
break;
}
OracleConnection vOracleConnection = new OracleConnection(vConnStr);
if (vOracleConnection.State != ConnectionState.Open)
{
vOracleConnection.Open();
}
return vOracleConnection;
}
private void CloseOracleDBConn(OracleConnection vOracleConnection)
{
if (vOracleConnection.State == ConnectionState.Open)
{
vOracleConnection.Close();
}
}
private OracleDataReader CreateOracleDataReader(string vSql)
{
OracleConnection vOracleConn = OpenOracleDBConn();
OracleCommand vOracleCommand = new OracleCommand(vSql, vOracleConn);
OracleDataReader vOracleDataReader = vOracleCommand.ExecuteReader();
return vOracleDataReader;
}
private OracleDataAdapter CreateOleDbDataAdapter(string vSql)
{
OracleConnection vOracleConn = OpenOracleDBConn();
OracleDataAdapter vOracleDataAdapter = new OracleDataAdapter(vSql, vOracleConn);
CloseOracleDBConn(vOracleConn);
return vOracleDataAdapter;
}
public string GetDateTimeNow()
{
return System.DateTime.Now.ToString("u").Replace("Z", "").Replace("z", "");
}
private void WriteLog(string vMessage)
{
try
{
string vTempValue = string.Empty;
string vFilePath = Application.StartupPath;
string vXmlPath = System.Configuration.ConfigurationManager.AppSettings["LogAddress"];
vXmlPath = vFilePath + vXmlPath;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(vXmlPath);
XmlNode root = xmlDoc.SelectSingleNode("//root");
XmlElement xe = xmlDoc.CreateElement("Node");//创建一个节点
XmlElement xesub01 = xmlDoc.CreateElement("RowNum");
xesub01.InnerText = root.ChildNodes.Count.ToString();
xe.AppendChild(xesub01);//添加到节点中
XmlElement xesub02 = xmlDoc.CreateElement("Message");
xesub02.InnerText = vMessage;
xe.AppendChild(xesub02);//添加到节点中
XmlElement xesub03 = xmlDoc.CreateElement("InserTime");
xesub03.InnerText = GetDateTimeNow();
xe.AppendChild(xesub03);//添加到节点中
root.AppendChild(xe);//添加到节点中
xmlDoc.Save(vXmlPath);
root = null;
xmlDoc = null;
}
catch (Exception ex)
{
WriteLog(ex.Message);
}
}
}

(0)

相关推荐

  • C# Oracle数据库操作类实例详解

    本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长度等,同时还有哈稀表自动插入数据库等高级任务.需要特别指出的是:在执行SQL语句,返回 DataReader之前一定要先用.read()打开,然后才能读到数据,再用hashTable对数据库进行insert,update,del操作,注意此时只能用默认的数据库连接"connstr". 完整

  • Oracle 数据库操作类

    复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Data.OracleClient; using System.Text; using System.Windows.Forms; using System.Xml; using Transactions; /// <summary> /// DB 的摘要说明 Written By Luos.Luo ,the creator

  • PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】

    本文实例讲述了PHP基于pdo的数据库操作类.分享给大家供大家参考,具体如下: 工作中需要操作sqlserver.oracle都是使用的这个类,当时是在别人的基础上改进了,现在分享下 <?php class Pdodb{ protected $pdo; protected $res; protected $config; /*构造函数*/ function __construct($config){ $this->Config = $config; $this->connect(); }

  • PHP封装的PDO数据库操作类实例

    本文实例讲述了PHP封装的PDO数据库操作类.分享给大家供大家参考,具体如下: <?php class DatabaseHandler { /** * sql语句查询 */ public static function query_data ($dataName,$sql,$query=array()) { $result = array(); if (!empty($sql)) { $data = Bj_PdoDB::factory($dataName)->allPrepare($sql,

  • mysql仿asp的数据库操作类

    CODE:[复制到剪切板]<?php class MySQLDB   {     //MYSQL数据库操作类     //作者:熊毅     //版本:2.0(发行版) //可以自由转载,修改请通知我scxy78@yeah.net     //转载请保留以上声明 //上进行操作,当然也可以每次指定特殊的表进行操作     //nErr指示是否操作出错,sErr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误     //错误之处请指正     //欢迎来信与我交流编程经验:scxy7

  • 一个ASP.NET的MYSQL的数据库操作类自己封装的

    /** * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility { /// <summary>对MYSQL数据库的操作类 /// /// </summary> public class MYSQLHelper { private MySqlConnection conn = null; private MyS

  • ASP.NET数据库操作类实例

    本文实例讲述了ASP.NET数据库操作类.分享给大家供大家参考,具体如下: 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 Syst

  • php简单数据库操作类的封装

    本文实例为大家分享了php封装一个简单的数据库操作类,供大家参考,具体内容如下 一.代码 <?php class ConnDB{ private $local; private $username; private $pwd; private $db; private $connID; private $code; public function __construct($local,$username,$pwd,$connID,$code,$db){ $this->local=$local;

  • php实现可用于mysql,mssql,pg数据库操作类

    本文实例讲述了可用mysql,mssql,pg三种数据库的数据库操作类,你只要作任何修改就可以方便的改变你数据库的类型.分享给大家供大家参考.具体分析如下: 函数清单,索引: Open:打开数据库连接 Line:71 Close:关闭数据库连接 Line:107 SelectDB:选择数据库 Line:129 Query:创建查询 Line:151 DataSeek:移动记录指针 Line:175 FieldName:获取字段名称 Line:198 FieldType:获取字段类型 Line:2

  • 一个基于PDO的数据库操作类(新) 一个PDO事务实例

    复制代码 代码如下: <?php /* * 作者:胡睿 * 日期:2011/03/19 * 电邮:hooray0905@foxmail.com * * 20110319 * 常用数据库操作,如:增删改查,获取单条记录.多条记录,返回最新一条插入记录id,返回操作记录行数等 * 20110630 * 整体修改方法,合并部分参数 * 规范代码,一个方法里只有1个return语句 */ /* 参数说明 int $debug 是否开启调试,开启则输出sql语句 int $mode 0 返回数组 1 返回

随机推荐