Oracle 数据库操作类

时间:2023-07-24 06:58:42 


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
投稿

猜你喜欢

  • 自定义django admin model表单提交的例子

    2023-03-16 13:11:43
  • Python格式化输出--%s,%d,%f的代码解析

    2022-10-31 06:26:44
  • python实时获取外部程序输出结果的方法

    2023-04-27 02:29:04
  • MYSQL使用.frm恢复数据表结构的实现方法

    2024-01-24 21:52:27
  • vue.js选中动态绑定的radio的指定项

    2024-04-27 16:13:25
  • 浅析Python 抽象工厂模式的优缺点

    2021-08-12 01:33:17
  • 一篇文章带你了解Python中的类

    2022-10-11 19:46:18
  • PyCharm+Qt Designer+PyUIC安装配置教程详解

    2024-01-04 12:58:02
  • Python编程之基于概率论的分类方法:朴素贝叶斯

    2023-10-08 10:15:52
  • Mysql覆盖索引详解

    2024-01-14 06:54:29
  • JavaScript框架比较:选择器

    2010-04-20 14:48:00
  • python 实现数字字符串左侧补零的方法

    2021-07-07 10:34:43
  • ASP操作XML文件的完整实例

    2007-09-26 12:05:00
  • Python实现读取邮箱中的邮件功能示例【含文本及附件】

    2022-10-25 04:37:23
  • mysql批量插入BulkCopy的实现

    2024-01-28 14:53:12
  • 带你彻底搞懂python操作mysql数据库(cursor游标讲解)

    2024-01-25 21:53:58
  • js重写alert事件(避免alert弹框标题出现网址)

    2024-05-08 09:32:43
  • python中的sys模块和os模块

    2021-07-06 00:43:17
  • SQL存储过程初探

    2009-09-09 14:22:00
  • Python+OpenCV实现图片及视频中选定区域颜色识别

    2021-04-10 03:36:26
  • asp之家 网络编程 m.aspxhome.com