注意,引用using Dm;
点击查看代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using Dm;
namespace Web.AppCode
{
public abstract class DbHelperOra
{
public static string connectionString = string.Empty;
static DbHelperOra()
{
try
{
connectionString = "Server=localhost; User Id=KING; PWD=123456789;port=5233;";
//cnn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#region 公用方法
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;
}
}
public static bool Exists(string strSql, params DmParameter[] 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;
}
}
#endregion
#region 执行简单SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (DmConnection connection = new DmConnection(connectionString))
{
using (DmCommand cmd = new DmCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (DmException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (DmConnection conn = new DmConnection(connectionString))
{
conn.Open();
DmCommand cmd = new DmCommand();
cmd.Connection = conn;
DbTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (DmException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
/// <param name="param">参数值</param>
public static void ExecuteSqlTranParam(ArrayList SQLStringList, params DmParameter[] param)
{
using (DmConnection conn = new DmConnection(connectionString))
{
conn.Open();
DmCommand cmd = new DmCommand();
cmd.Connection = conn;
DbTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
PrepareCommandList(cmd, conn, tx, SQLStringList, param);
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (DmException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString)
{
using (DmConnection connection = new DmConnection(connectionString))
{
using (DmCommand cmd = new DmCommand(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 (DmException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// 执行查询语句,返回DbDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>DmDataReader</returns>
public static DbDataReader ExecuteReader(string strSQL)
{
DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand(strSQL, connection);
try
{
connection.Open();
DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (DmException e)
{
throw new Exception(e.Message);
}
}
/// <summary>完成数据的查询,返回DataTable
///
/// </summary>
/// <param name="Dm">要执行的Dm</param>
/// <param name="param">参数</param>
/// <returns>DataTable</returns>
public static DataTable GetTable(string sql, params DmParameter[] param)
{
//实例化连接对象,并指定连接字符串,自动释放资源,不用关闭
using (DmConnection conn = new DmConnection(connectionString))
{
DmDataAdapter adp = new DmDataAdapter(sql, conn);
if (param != null)
{
adp.SelectCommand.Parameters.AddRange(param);
}
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (DmConnection connection = new DmConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
DmDataAdapter command = new DmDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (DmException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
#endregion
#region 执行带参数的SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString, params DmParameter[] cmdParms)
{
using (DmConnection connection = new DmConnection(connectionString))
{
using (DmCommand cmd = new DmCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (DmException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的DmParameter[])</param>
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (DmConnection conn = new DmConnection(connectionString))
{
conn.Open();
using (DbTransaction trans = conn.BeginTransaction())
{
DmCommand cmd = new DmCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
DmParameter[] cmdParms = (DmParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString, params DmParameter[] cmdParms)
{
using (DmConnection connection = new DmConnection(connectionString))
{
using (DmCommand cmd = new DmCommand())
{
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 (DmException e)
{
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// 执行查询语句,返回DbDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>DmDataReader</returns>
public static DbDataReader ExecuteReader(string SQLString, params DmParameter[] cmdParms)
{
DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (DmException e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString, params DmParameter[] cmdParms)
{
using (DmConnection connection = new DmConnection(connectionString))
{
DmCommand cmd = new DmCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (DmDataAdapter da = new DmDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (DmException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="cmdText">查询语句</param>
/// <param name="commandParameters">参数集合</param>
/// <returns>返回DataTable</returns>
public static DataTable ExecuteDataTable(string cmdText, params DmParameter[] commandParameters)
{
DataTable dt = new DataTable();
DmCommand cmd = new DmCommand();
using (DmConnection connection = new DmConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdText, commandParameters);
DmDataAdapter da = new DmDataAdapter(cmd);
da.Fill(dt);
cmd.Parameters.Clear();
cmd.Dispose();
return dt;
}
}
/// <summary> 参数格式化批量删除
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="conn"></param>
/// <param name="trans">事务</param>
/// <param name="lst">多条sql语句</param>
/// <param name="cmdParms">param</param>
private static void PrepareCommandList(DmCommand cmd, DmConnection conn, DbTransaction trans, ArrayList lst, DmParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = lst.ToString();
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (DmParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
private static void PrepareCommand(DmCommand cmd, DmConnection conn, DbTransaction trans, string cmdText, DmParameter[] 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 (DmParameter parm in cmdParms)
{
if (parm.Value == null || parm.Value.ToString() == "")
{
parm.Value = DBNull.Value;
}
cmd.Parameters.Add(parm);
}
}
}
#endregion
#region 存储过程操作
/// <summary>
/// 执行存储过程 返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>DmDataReader</returns>
public static DbDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
DmConnection connection = new DmConnection(connectionString);
DbDataReader returnReader;
connection.Open();
DmCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="tableName">DataSet结果中的表名</param>
/// <returns>DataSet</returns>
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
{
using (DmConnection connection = new DmConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
DmDataAdapter sqlDA = new DmDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
/// <summary>
/// 构建 DmCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>DmCommand</returns>
private static DmCommand BuildQueryCommand(DmConnection connection, string storedProcName, IDataParameter[] parameters)
{
DmCommand command = new DmCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (DmParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
/// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="rowsAffected">影响的行数</param>
/// <returns></returns>
public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (DmConnection connection = new DmConnection(connectionString))
{
int result;
connection.Open();
DmCommand command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}
/// <summary>
/// 创建 DmCommand 对象实例(用来返回一个整数值)
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>DmCommand 对象实例</returns>
private static DmCommand BuildIntCommand(DmConnection connection, string storedProcName, IDataParameter[] parameters)
{
DmCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new DmParameter("ReturnValue",
DmDbType.Int32, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
#endregion
}
}