|
楼主 |
发表于 2012-7-7 16:16:36
|
显示全部楼层
dafeng 发表于 2012-7-7 16:15
四、代码
1、DbHelp.cs
/*
2、DbBase.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Yike.Common
{
public class DbBase : IDbConnection, IDb
{
public static DbConnection conn;
public ConnectionStringSettings ConnectionStringSetting
{
get;
set;
}
#region IDbConnection 成员
public IDbTransaction BeginTransaction(IsolationLevel il)
{
return conn.BeginTransaction(il);
}
public IDbTransaction BeginTransaction()
{
return conn.BeginTransaction();
}
public virtual void ChangeDatabase(string databaseName)
{
}
public void Close()
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
public string ConnectionString
{
get
{
return conn.ConnectionString;
}
set
{
conn.ConnectionString = value;
}
}
public int ConnectionTimeout
{
get { return conn.ConnectionTimeout; }
}
public IDbCommand CreateCommand()
{
return conn.CreateCommand();
}
public virtual IDbDataAdapter CreateDataAdapter()
{
return null;
}
string IDbConnection.Database
{
get { return conn.Database; }
}
public void Open()
{
if (conn.State != ConnectionState.Open)
conn.Open();
}
public ConnectionState State
{
get { return conn.State; }
}
#endregion
#region IDisposable 成员
public void Dispose()
{
conn.Close();
conn.Dispose();
}
#endregion
#region Command
/// <summary>
/// 执行查询,返回影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteNonQuery(string sql)
{
using (IDbCommand com = CreateCommand())
{
com.CommandText = sql;
return com.ExecuteNonQuery();
}
}
/// <summary>
/// 执行查询,返回影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int ExecuteNonQuery(string sql, List<DbParameter> parameters)
{
using (IDbCommand com = CreateCommand())
{
com.CommandText = sql;
if (parameters == null || parameters.Count == 0)
return 0;
foreach (DbParameter parameter in parameters)
com.Parameters.Add(parameter);
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
return com.ExecuteNonQuery();
}
}
/// <summary>
/// 执行查询,返回影响的行数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="entity"></param>
/// <returns></returns>
public int ExecuteNonQuery<T>(string sql, T entity) where T : class, new()
{
using (IDbCommand com = CreateCommand())
{
GetParameters(com, sql, entity);
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
return com.ExecuteNonQuery();
}
}
/// <summary>
/// 执行查询返回第一行第一列的数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public object ExecuteScalar(string sql)
{
using (IDbCommand com = CreateCommand())
{
com.CommandText = sql;
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
return com.ExecuteScalar();
}
}
/// <summary>
/// 执行查询返回第一行第一列的数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public object ExecuteScalar(string sql, List<DbParameter> parameters)
{
using (IDbCommand com = CreateCommand())
{
com.CommandText = sql;
if (parameters == null || parameters.Count == 0)
return null;
foreach (DbParameter parameter in parameters)
com.Parameters.Add(parameter);
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
return com.ExecuteScalar();
}
}
/// <summary>
/// 执行查询返回第一行第一列的数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="entity"></param>
/// <returns></returns>
public object ExecuteScalar<T>(string sql, T entity) where T : class, new()
{
using (IDbCommand com = CreateCommand())
{
GetParameters(com, sql, entity);
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
return com.ExecuteScalar();
}
}
#endregion
#region Tools
/// <summary>
/// 根据sql和实体获取参数
/// </summary>
/// <param name="sql"></param>
/// <param name="entity"></param>
/// <returns></returns>
public virtual List<DbParameter> GetParameters<T>(IDbCommand com, string sql, T entity)
{
return null;
}
public IEnumerator GetParameterNames(string sql, Regex regex)
{
sql = Regex.Replace(sql, @"['][\S]*[']", "");
MatchCollection matchs = regex.Matches(sql);
return matchs.GetEnumerator();
}
/// <summary>
/// 根据数据库获取参数名称
/// </summary>
/// <param name="aFieldName">字段名称</param>
/// <returns></returns>
public virtual string GetParameterName(string fieldName)
{
return null;
}
public virtual DbParameter GetParameter(string name, object value)
{
return null;
}
#endregion
#region Query
/// <summary>
/// 将表转换为实体
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="dt">数据</param>
/// <returns></returns>
public List<T> Query<T>(string sql) where T : class, new()
{
return Query<T>(sql, new T());
}
/// <summary>
/// 将表转换为实体
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="dt">数据</param>
/// <returns></returns>
public DataSet QueryDS(string sql)
{
using (IDbCommand com = CreateCommand())
{
DataSet ds = new DataSet();
com.CommandText = sql;
IDbDataAdapter da = CreateDataAdapter();
da.SelectCommand = com;
if (State != ConnectionState.Open)
throw new Exception("Database not opened");
da.Fill(ds);
return ds;
}
}
|
|