Oracle 数据库操作类_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);
}
}
}

时间: 2024-10-13 00:44:33

Oracle 数据库操作类_oracle的相关文章

asp.net 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 Sale

谁有asp.net连接oracle数据库操作类,要存储过程模式的?

问题描述 谁有asp.net连接oracle数据库操作类,要存储过程模式的? 解决方案 解决方案二:这个还没有用过,帮你up把解决方案三:我也没有用过oracle

Oracle数据库操作类(c#)

oracle|数据|数据库 用OracleConnection连接Oracle在Web.config中配置 <add key="Oracle" value="Data Source=xxx;User ID=xxx;Password=xxx"/> public System.Data.OracleClient.OracleConnection cnn; /// <summary> /// 打开数据库连接 /// </summary>

Oracle 数据库操作技巧集_oracle

正在看的ORACLE教程是:Oracle 数据库操作技巧集.[编者注:]提起数据库,第一个想到的公司,一般都会是Oracle(即甲骨文公司).Oracle在数据库领域一直处于领先地位.Oracle关系数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好.使用方便.功能强,适用于各类大.中.小.微机环境.Oracle9i是Oracle于今年6月份正式推出的数据库最新产品.Oracle9i在可伸缩性.可靠性和完整性方面有着上佳的表现,一推出就获得了开发者的认同.它是一种高效率.可靠性好的适

PHP实现的一个简单的数据库操作类

PHP实现的一个简单的数据库操作类 实现的功能: - 在实例化的时候能设置连接字符集 - 在实例化的时候能连接数据库 - 在实例化的时候能选择默认数据库 - 销毁对象时关闭数据库 代码如下: <?php // 数据库操作类MySQLDB class MySQLDB { // 声明属性 private $server; private $username; private $password; public $default_db; public $link; // 声明构造函数 public f

ASP通用数据库操作类源代码

<%'=========================================================================='文件名称:clsDbCtrl.asp'功 能:数据库操作类'作 者:coldstone (coldstone[在]qq.com)'程序版本:v1.0.5'完成时间:2005.09.23'修改时间:2007.10.30'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息.'          如果你修改了程序中的代码并得到更好的应用,

asp数据库操作类

<%'=========================================================================='文件名称:clsDbCtrl.asp'功 能:数据库操作类'作 者:coldstone (coldstone[在]qq.com)'程序版本:v1.0.5'完成时间:2005.09.23'修改时间:2007.10.30'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息.' 如果你修改了程序中的代码并得到更好的应用,请发送一份给我,谢

最好用的PHP数据库操作类-ezSQL

 ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的MySQL部分.该数据库操作类支持几乎所有主流的数据库,如:PHP-PDO, mySQL, Oracle, InterBase/FireBird, PostgreSQL, SQLite以及MS-SQL等.ezSQL具有很强的调试功能,可以快速地查看SQL代码的执行情况.使用ezSQL,可以为我们节省开发时间.简化代码并提高运行效率. ezSQL的优点就不用多说了,它小巧.快速.简单.易

Oricle 数据库操作类

Oricle 数据库操作类 define('OCI_RETURN_ALLS',OCI_BOTH + OCI_RETURN_LOBS); class Oracle {  /**   * select方法返回的最大记录数   */  const MAX_ROW_NUM = 1000;  /**   * 数据查询结果集对象   * @var object $dataSet   */  public $dataSet   = NULL ;  /**   * 数据源对象   * @var object $