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.

277 lines
7.4 KiB
C#

using System.Data;
using System;
using System.Data.Common;
namespace Mesnac.Codd.Session
{
/// <summary>
/// 数据库操作类
/// </summary>
public class DbHelper
{
public DbHelper(DbSession dbSession)
{
this.dbSession = dbSession;
}
#region 私有变量
private bool closeByHandle = false;
private DbSession dbSession = null;
private DbTransaction transaction = null;
#endregion
#region 属性
public DbSession DbSession
{
get { return this.dbSession; }
}
public CommandType CommandType
{
get
{
return this.dbSession.Command.CommandType;
}
set
{
this.dbSession.Command.CommandType = value;
}
}
public string CommandText
{
get
{
return this.dbSession.Command.CommandText;
}
set
{
this.dbSession.Command.CommandText = value;
}
}
#endregion
#region 方法
private DbType ToDbType(object o)
{
Type t = o.GetType();
DbType dbt = DbType.Object;
try
{
dbt = (DbType)Enum.Parse(typeof(DbType), t.Name);
}
catch
{
}
return dbt;
}
public void ClearParameter()
{
this.dbSession.Command.Parameters.Clear();
}
public DbParameter CreateParameter(string key, object value)
{
DbParameter Result = this.dbSession.ProviderFactory.CreateParameter();
Result.ParameterName = key;
if (value == null || value == System.DBNull.Value)
{
Result.Value = System.DBNull.Value;
}
else
{
Result.DbType = ToDbType(value);
Result.Value = value;
}
return Result;
}
public DbParameter CreateParameter(string key)
{
DbParameter Result = this.dbSession.ProviderFactory.CreateParameter();
Result.ParameterName = key;
return Result;
}
public void AddParameter(string key, object value)
{
this.dbSession.Command.Parameters.Add(CreateParameter(key, value));
}
public void AddParameter(DbParameter para)
{
this.dbSession.Command.Parameters.Add(para);
}
public void OpenConnection(bool closeByHandle)
{
this.closeByHandle = closeByHandle;
try
{
if (this.dbSession.Connection.State != ConnectionState.Open)
{
this.dbSession.Connection.Open();
}
}
catch (Exception ex)
{
//ICSharpCode.Core.LoggingService.Error("打开数据连接失败!");
ICSharpCode.Core.LoggingService.Error(ex.Message);
//ICSharpCode.Core.LoggingService.Error(ex.StackTrace);
}
}
public void OpenConnection()
{
this.OpenConnection(false);
}
public void CloseConnection()
{
try
{
this.dbSession.Connection.Close();
this.dbSession.Connection.Dispose();
this.dbSession.Command.Dispose();
this.dbSession.Close();
}
catch { }
}
private void AutoCloseConnection()
{
ClearParameter();
if (this.closeByHandle) { return; }
if (this.transaction != null) { return; }
if (this.dbSession.Command.Transaction != null) { return; }
CloseConnection();
}
public void ExecuteNonQuery()
{
try
{
this.OpenConnection();
this.dbSession.Command.ExecuteNonQuery();
}
finally
{
this.AutoCloseConnection();
}
}
public object ToDataSet()
{
try
{
this.OpenConnection();
DataSet Result = new DataSet();
DbDataAdapter dataAdapter = this.dbSession.ProviderFactory.CreateDataAdapter();
dataAdapter.SelectCommand = this.dbSession.Command;
dataAdapter.Fill(Result);
return Result;
}
finally
{
this.AutoCloseConnection();
}
}
public DataTable ToDataTable()
{
try
{
this.OpenConnection();
DataTable Result = new DataTable();
DbDataReader myReader = this.ToDbDataReader();
if (myReader == null)
{
return new DataTable();
}
Result.Load(myReader, LoadOption.Upsert);
return Result;
}
finally
{
this.AutoCloseConnection();
}
}
public object ToScalar()
{
try
{
this.OpenConnection();
return this.dbSession.Command.ExecuteScalar();
}
finally
{
this.AutoCloseConnection();
}
}
public DbDataReader ToDbDataReader()
{
try
{
this.OpenConnection();
return this.dbSession.Command.ExecuteReader();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("获取DataReader失败!");
ICSharpCode.Core.LoggingService.Error(ex.Message);
//ICSharpCode.Core.LoggingService.Error(ex.StackTrace);
this.AutoCloseConnection();
return null;
}
finally
{
this.ClearParameter();
}
}
public DbTransaction BeginTransaction()
{
try
{
this.OpenConnection();
if (this.transaction == null)
{
this.transaction = this.dbSession.Connection.BeginTransaction();
}
this.dbSession.Command.Transaction = this.transaction;
return this.transaction;
}
finally
{
this.AutoCloseConnection();
}
}
public void RollbackTransaction()
{
try
{
if (this.transaction != null)
{
this.transaction.Rollback();
}
}
finally
{
this.transaction = null;
this.AutoCloseConnection();
}
}
public void CommitTransaction()
{
try
{
if (this.transaction != null)
{
this.transaction.Commit();
}
}
finally
{
this.transaction = null;
this.AutoCloseConnection();
}
}
#endregion
}
}