You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
15 KiB
C#

10 months ago
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace WebSqlHelper
{
public class DbHelper
{
protected Database db;
protected Database CreateDatabase(string name)
{
return db = DatabaseFactory.CreateDatabase(name);
}
protected Database CreateDatabase()
{
return db = DatabaseFactory.CreateDatabase();
}
public DbHelper(string name)
{
//db = DatabaseFactory.CreateDatabase(name);
}
public DbHelper()
{
//db = DatabaseFactory.CreateDatabase();
}
public virtual DataSet LoadDataSet(string[] table, string str, params DbParameter[] param)
{
DbCommand cmd = db.GetSqlStringCommand(str);
cmd.Parameters.AddRange(param);
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, table);
return ds;
}
#region Parameter
public virtual DbParameter GetParameter()
{
return db.DbProviderFactory.CreateParameter();
}
/// <summary>
/// Configures a given <see cref="DbParameter"/>.
/// </summary>
/// <param name="param">The <see cref="DbParameter"/> to configure.</param>
/// <param name="name"><para>The name of the parameter.</para></param>
/// <param name="dbType"><para>One of the <see cref="DbType"/> values.</para></param>
/// <param name="size"><para>The maximum size of the data within the column.</para></param>
/// <param name="direction"><para>One of the <see cref="ParameterDirection"/> values.</para></param>
/// <param name="nullable"><para>Avalue indicating whether the parameter accepts <see langword="null"/> (<b>Nothing</b> in Visual Basic) values.</para></param>
/// <param name="precision"><para>The maximum number of digits used to represent the <paramref name="value"/>.</para></param>
/// <param name="scale"><para>The number of decimal places to which <paramref name="value"/> is resolved.</para></param>
/// <param name="sourceColumn"><para>The name of the source column mapped to the DataSet and used for loading or returning the <paramref name="value"/>.</para></param>
/// <param name="sourceVersion"><para>One of the <see cref="DataRowVersion"/> values.</para></param>
/// <param name="value"><para>The value of the parameter.</para></param>
protected virtual void ConfigureParameter(DbParameter param, string name, DbType dbType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
{
param.ParameterName = name;
param.DbType = dbType;
param.Size = size;
param.Value = (value == null) ? DBNull.Value : value;
param.Direction = direction;
param.IsNullable = nullable;
param.SourceColumn = sourceColumn;
param.SourceVersion = sourceVersion;
}
public virtual DbParameter GetParameter(string name, object value)
{
DbParameter param = GetParameter();
ConfigureParameter(param, name, DbType.String, 0, ParameterDirection.Input, true, 0, 0, String.Empty, DataRowVersion.Default, value);
return param;
}
public virtual DbParameter GetParameter(string name, object value, DbType dbtype)
{
DbParameter param = GetParameter();
ConfigureParameter(param, name, dbtype, 0, ParameterDirection.Input, true, 0, 0, String.Empty, DataRowVersion.Default, value);
return param;
}
#endregion
public virtual DbCommand GetSqlStringCommand(string query, params DbParameter[] param)
{
DbCommand cmd = db.GetSqlStringCommand(query);
cmd.Parameters.Clear();
if (param != null && param.Length > 0)
{
foreach (DbParameter pr in param)
cmd.Parameters.Add(GetParameter(pr.ParameterName, pr.Value, pr.DbType));
}
return cmd;
}
#region 返回数据
#region 通过Sql语句返回数据
public virtual DataSet GetSqlStrDataSet(string query, params DbParameter[] param)
{
return GetSqlStrDataSet(null, query, param);
}
public virtual DataSet GetSqlStrDataSet(DbTransaction tran, string query, params DbParameter[] param)
{
try
{
if (tran != null)
return db.ExecuteDataSet(GetSqlStringCommand(query, param), tran);
else
return db.ExecuteDataSet(GetSqlStringCommand(query, param));
}
catch (Exception ee)
{
throw new Exception(ee.Message + query);
}
}
public virtual DataTable GetSqlStrTable(string query, params DbParameter[] param)
{
return GetSqlStrTable(null, query, param);
}
public virtual DataTable GetSqlStrTable(DbTransaction tran, string query, params DbParameter[] param)
{
DataSet ds = GetSqlStrDataSet(tran, query, param);
if (ds != null && ds.Tables.Count > 0)
return ds.Tables[0];
return null;
}
public virtual DataRow GetSqlStrRow(string query, params DbParameter[] param)
{
return GetSqlStrRow(null, query, param);
}
public virtual DataRow GetSqlStrRow(DbTransaction tran, string query, params DbParameter[] param)
{
DataSet ds = GetSqlStrDataSet(tran, query, param);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
return ds.Tables[0].Rows[0];
return null;
}
public virtual object GetSqlStrScalar(string query, params DbParameter[] param)
{
return GetSqlStrScalar(null, query, param);
}
public virtual object GetSqlStrScalar(DbTransaction tran, string query, params DbParameter[] param)
{
if (tran != null)
return db.ExecuteScalar(GetSqlStringCommand(query, param), tran);
else
return db.ExecuteScalar(GetSqlStringCommand(query, param));
}
#endregion
#region 通过存储过程语句返回数据
public virtual DataSet GetSpDataSet(string proc, params object[] param)
{
return GetSpDataSet(null, proc, param);
}
public virtual DataSet GetSpDataSet(DbTransaction tran, string proc, params object[] param)
{
//object[] paras = (param!=null?new object[param.Length ]:new object[1]);
//for(int i=0;i<param.Length;i++)
//{
// paras[i] = param[i];
// if (paras[i] != null && paras[i].ToString() == "")
// paras[i] = null;
//}
//if(param!=null)
// paras[param.Length] = "";
//else
//paras[0] = "";
if (tran != null)
return db.ExecuteDataSet(tran, proc, param);
else
return db.ExecuteDataSet(proc, param);
}
public virtual DataTable GetSpTable(string query, params object[] param)
{
return GetSpTable(null, query, param);
}
public virtual DataTable GetSpTable(DbTransaction tran, string query, params object[] param)
{
DataSet ds = GetSpDataSet(tran, query, param);
if (ds != null && ds.Tables.Count > 0)
return ds.Tables[0];
return null;
}
public virtual DataRow GetSpRow(string query, params object[] param)
{
return GetSpRow(null, query, param);
}
public virtual DataRow GetSpRow(DbTransaction tran, string query, params object[] param)
{
DataSet ds = GetSpDataSet(tran, query, param);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
return ds.Tables[0].Rows[0];
return null;
}
public virtual object GetSpScalar(string query, params object[] param)
{
return GetSpScalar(null, query, param);
}
public virtual object GetSpScalar(DbTransaction tran, string query, params object[] param)
{
if (tran != null)
return db.ExecuteScalar(query, param, tran);
else
return db.ExecuteScalar(query, param);
}
#endregion
#endregion
#region 直接执行
public virtual int SetSqlStrNonQuery(string query, params DbParameter[] param)
{
return SetSqlStrNonQuery(null, query, param);
}
public virtual int SetSqlStrNonQuery(string query, LogHelper Opertor, params DbParameter[] param)
{
return SetSqlStrNonQuery(null, query, Opertor, param);
}
public virtual int SetSqlStrNonQuery(DbTransaction tran, string query, LogHelper Opertor, params DbParameter[] param)
{
//记录日志的执行方法
//@Opertor 操作信息
int result = 0;
DbCommand dbcomm = GetSqlStringCommand(query, param);
try
{
if (tran != null)
result = db.ExecuteNonQuery(dbcomm, tran);
else
result = db.ExecuteNonQuery(dbcomm);
//无执行错误,记录日志
Opertor.ExecLoger(dbcomm);
return result;
}
catch (Exception ex)
{
throw ex;
}
}
public virtual int SetSqlStrNonQuery(DbTransaction tran, string query, params DbParameter[] param)
{
DbCommand comm = GetSqlStringCommand(query, param);
if (tran != null)
return db.ExecuteNonQuery(GetSqlStringCommand(query, param), tran);
else
return db.ExecuteNonQuery(GetSqlStringCommand(query, param));
}
public virtual int SetSpNonQuery(string proc, params object[] param)
{
return db.ExecuteNonQuery(proc, param);
}
public virtual int SetSpNonQuery(DbTransaction tran, string proc, params DbParameter[] param)
{
if (tran != null)
return db.ExecuteNonQuery(proc, param, tran);
else
return db.ExecuteNonQuery(proc, param);
}
#endregion
#region Connection,Transaction
public virtual DbConnection CreateConnection()
{
return db.CreateConnection();
}
public class TransWraper
{
public string Sql;
public List<DbParameter> Param;
public TransWraper(string sql, params DbParameter[] param)
{
Sql = sql;
if (param != null && param.Length > 0)
{
Param = new List<DbParameter>();
Param.AddRange(param);
}
}
}
public virtual void ExecuteTran(List<TransWraper> query)
{
using (DbConnection conn = CreateConnection())
{
conn.Open();
DbTransaction tran = conn.BeginTransaction();
try
{
foreach (TransWraper tw in query)
{
if (tw != null)
SetSqlStrNonQuery(tran, tw.Sql, tw.Param.ToArray());
else
SetSqlStrNonQuery(tran, tw.Sql);
}
//SetSqlStrTranNonQuery(query, param);
tran.Commit();
}
catch
{
tran.Rollback();
}
}
}
#endregion
#region 分页函数
public virtual DataTable ExecutePager(string tbname, string fields, string where, string orderby, int pagesize, int pageindex, ref int recordcount)
{ return null; }
public virtual DataTable ExecutePager(string tbname, string fields, string where, string orderby, int pagesize, int pageindex, System.Data.Common.DbParameter[] param, ref int recordcount)
{ return null; }
#endregion
#region 自增
public virtual int GetNext(string table, string field)
{
return GetNext(null, table, field);
}
public virtual int GetNext(DbTransaction tran, string table, string field)
{
object c = GetSqlStrScalar("select max(nvl2(translate(" + field + ",'\\1234567890','\\'),0," + field + ")) from " + table);
if (c == null || c is DBNull)
return 1;
return int.Parse(c.ToString()) + 1;
}
public Guid GetNext()
{
return Guid.NewGuid();
}
public virtual string GetNext(DbTransaction tran, string table, string field, string format)
{
switch (format)
{
case "D":
case "d"://080128
object c = GetSqlStrScalar("select max(case when ISNUMERIC(" + field + ")=1 then " + field + " else 0 end) from " + table + " where " + field + " like '" + DateTime.Now.ToString("yyMMdd") + "%'");
if (c == null || c is DBNull)
return DateTime.Now.ToString("yyMMdd") + "001";
else
{
string s1 = c.ToString().Substring(0, 6);
string s2 = c.ToString().Substring(6);
return s1 + (Int64.Parse(s2) + 1).ToString().PadLeft(3, '0');
}
break;
}
return GetNext(tran, table, field).ToString();
}
#endregion
public virtual bool IsExist(string table, string field, string value)
{
return Convert.ToInt16(GetSqlStrScalar("select count(*) from " + table + " where " + field + "=@" + field, GetParameter(field, value))) > 0;
}
#region yjy 执行默认的sql文本
//public virtual int SetSqlStrNonQuery(string query)
//{
// return SetSqlStrNonQuery(null, query);
//}
//public virtual int SetSqlStrNonQuery(DbTransaction tran, string query)
//{
// if (tran != null)
// return db.ExecuteNonQuery(GetSqlStringCommand(query,null),tran);
// else
// return db.ExecuteNonQuery( GetSqlStringCommand(query,null));
//}
#endregion
}
}