Oracle 数据库操作类

来源:asp之家 时间:2009-08-12 12:06:00 

代码如下:


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);
}
}
}

标签:oracle,数据库,操作类
0
投稿

猜你喜欢

  • sqlserver中操作主从关系表的sql语句

    2024-01-22 09:13:14
  • SQL Server AlwaysOn读写分离配置图文教程

    2024-01-19 10:20:20
  • 探究Python多进程编程下线程之间变量的共享问题

    2023-09-27 15:42:47
  • Pyscript使用本地Pyodide配置步骤

    2021-06-12 06:58:49
  • 全民学编程之 Hello World

    2023-03-13 20:48:37
  • JS重现80后儿时经典拼板(模板)游戏

    2011-09-11 18:36:46
  • python网络爬虫之模拟登录 自动获取cookie值 验证码识别的具体实现

    2023-10-08 03:00:37
  • python开启debug模式的方法

    2023-05-10 02:58:31
  • 一篇文章彻底搞懂python正则表达式

    2021-02-10 13:16:19
  • golang 的string与[]byte转换方式

    2024-03-08 20:01:40
  • Python3从零开始搭建一个语音对话机器人的实现

    2023-11-15 05:52:12
  • 数据库中聚簇索引与非聚簇索引的区别[图文]

    2012-02-25 19:38:23
  • 使用JS的DOM文档对象模型获取前端循环的参数

    2024-04-10 13:54:46
  • python在命令行下使用google翻译(带语音)

    2023-06-02 13:47:17
  • selenium.webdriver中add_argument方法常用参数表

    2023-12-05 13:39:38
  • python中os模块和sys模块的使用详解

    2021-08-29 21:42:35
  • Django中modelform组件实例用法总结

    2023-09-28 14:35:49
  • Django利用LogEntry生成历史操作实战记录

    2021-10-23 04:23:58
  • python 实现一个简单的线性回归案例

    2023-05-08 23:40:25
  • python画柱状图--不同颜色并显示数值的方法

    2021-12-31 17:22:18
  • asp之家 网络编程 m.aspxhome.com