using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
namespace jdjxcbase
{
public class HelpOracle
{
private static string connectionString = ConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString;
/// <summary>
/// 构造函数
/// </summary>
public HelpOracle()
{
}
/// <summary>
/// 获取表中最大ID+1
/// </summary>
/// <param name="FieldName">字段名称</param>
/// <param name="TableName">表名</param>
/// <returns></returns>
public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="strSql">sql语句</param>
/// <returns></returns>
public static bool Exists(string strSql)
{
object obj = GetSingle(strSql);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="strSql">sql语句</param>
/// <param name="cmdParms">参数</param>
/// <returns></returns>
public static bool Exists(string strSql, params OracleParameter[] cmdParms)
{
object obj = GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.OracleClient.OracleException E)
{
baseLog.error("执行sql出错 sql=" + SQLString);
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.OracleClient.OracleException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (OracleConnection conn = new OracleConnection(connectionString))
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
string strsql = "";
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.OracleClient.OracleException E)
{
tx.Rollback();
throw new Exception(E.Message + " " + strsql);
}
}
}
/// <summary>
/// 获取连接连接获取时已打开
/// </summary>
/// <returns></returns>
public static OracleConnection getConnection()
{
var conn = new OracleConnection(connectionString);
conn.Open();
return conn;
}
/// <summary>
/// 打开oracle连接的事务
/// </summary>
/// <param name="conn"></param>
/// <returns></returns>
public static OracleTransaction getTransaction(OracleConnection conn)
{
return conn.BeginTransaction();
}
/// <summary>
/// 销毁连接
/// </summary>
/// <param name="conn"></param>
public static void DisposeOracleConnection(OracleConnection conn)
{
conn.Dispose();
}
/// <summary>
/// 带事务查询
/// </summary>
/// <param name="sql"></param>
/// <param name="conn"></param>
/// <param name="tran"></param>
/// <param name="cmdParms"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static DataSet queryWithTransaction(string sql, OracleConnection conn, OracleTransaction tran, params OracleParameter[] cmdParms)
{
if (conn == null)
{
return null;
}
using (OracleCommand cmd = new OracleCommand())
{
PrepareCommand(cmd, conn, tran, sql, cmdParms);
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
da.SelectCommand.Transaction = tran;
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.OracleClient.OracleException ex)
{
baseLog.error("message=" + ex.Message + ",sql=" + sql);
throw new Exception(ex.Message);
}
return ds;
}
}
}
/// <summary>
/// 带事务执行
/// </summary>
/// <param name="sql"></param>
/// <param name="conn"></param>
/// <param name="tran"></param>
/// <param name="cmdParms"></param>
/// <exception cref="Exception"></exception>
public static int executeWithTransaction(string sql, OracleConnection conn, OracleTransaction tran, params OracleParameter[] cmdParms)
{
using (OracleCommand cmd = new OracleCommand())
{
PrepareCommand(cmd, conn, tran, sql, cmdParms);
try
{
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.OracleClient.OracleException ex)
{
baseLog.error("message=" + ex.Message + ",sql=" + sql);
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.OracleClient.OracleException ex)
{
baseLog.error("执行sql 出错" + SQLString);
throw new Exception(ex.Message);
}
return ds;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.OracleClient.OracleException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.OracleClient.OracleException e)
{
connection.Close();
baseLog.error(e.Message + SQLString);
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString, params OracleParameter[] cmdParms)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
}
}
}
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
}
}
二、数据库工具类的使用(这里已oracle数据库为示例)
//用using使用connection
using(var conn = HelpOracle.getConnection())
{
//这里需要把事务放获取异常外
var trans = HelpOracle.getTransaction(conn);
try
{
HelpOracle.executeWithTransaction("UPDATE USER SET INITPWD ='456' WHERE USERNAME ='it008'", conn, trans, null);
var ds = HelpOracle.queryWithTransaction("select INITPWD from USER where USERNAME ='it008'", conn, trans, null);
if (ds.Tables[0].Rows.Count > 0)
{
baseLog.info("事务中的初始密码是" + ds.Tables[0].Rows[0]["INITPWD"].ToString());
}
var ds1 = HelpOracle.Query("select INITPWD from USER where USERNAME ='it008'");
if (ds1.Tables[0].Rows.Count > 0)
{
baseLog.info("非事务中的初始密码是" + ds1.Tables[0].Rows[0]["INITPWD"].ToString());
}
if (ds.Tables[0].Rows[0]["INITPWD"].ToString() == "456")
{
HelpOracle.executeWithTransaction("UPDATE USER SET INITPWD ='567' WHERE USERNAME ='it008'", conn, trans, null);
}
trans.Commit();
}
catch
{
trans.Rollback();
}
}