namespace Yike.Common
{
public class DbHelp
{
public string DatabaseName
{
get;
set;
}
public DbHelp(string databaseName)
{
DatabaseName = databaseName;
}
public DbHelp()
{
DatabaseName = ConfigurationManager.AppSettings["defaultDb"].ToString();
}
public IDb Generate()
{
ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[DatabaseName];
switch (connectionStringSettings.ProviderName)
{
case "System.Data.OracleClient": return new Oracle(connectionStringSettings);
case "System.Data.SqlClient": return new Mssql(connectionStringSettings);
case "System.Data.OleDb": return new Access(connectionStringSettings);
default: throw new Exception("Not support Database");
}
}
}
}作者: dafeng 时间: 2012-7-7 16:16
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