using System; using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Collections;
using System.Web;
using System.Configuration;
namespace TestDb.Common {
/// <summary>
/// 数据库工厂
/// </summary>
public class DBFactory : IDisposable
{
/// <summary>
/// 数据库类型
/// SQLSERVER
/// ORACLE
/// OLEDB
/// </summary>
public enum DataBaseType
{
SQLSERVER,
ORACLE,
OLEDB
}
internal static readonly DataBaseType dbType = DataBaseType.SQLSERVER; internal static readonly string HTTPCONTEXTDB = "HTTPCONTEXTDB"; internal static readonly string HTTPCONTEXTDBTransaction = "HTTPCONTEXTDBTransaction";
public void Dispose(){}
/// <summary>
/// 取得连接对象
/// </summary>
public static IDbConnection Connection
{
get
{
if (HttpContext.Current.Items[HTTPCONTEXTDB] == null) {
switch (dbType)
{
case DataBaseType.SQLSERVER:
HttpContext.Current.Items[HTTPCONTEXTDB] = CreateConnection();
break;
}
}
return HttpContext.Current.Items[HTTPCONTEXTDB] as IDbConnection;
}
}
/// <summary>
/// 取得新的连接对象
/// </summary>
private static IDbConnection CreateConnection()
{
switch (dbType)
{
case DataBaseType.SQLSERVER:
SqlConnection connection = new SqlConnection(ApplicationConfiguration.ConnectionString);
connection.Open();
return connection;
}
return null;
}
/// <summary>
/// 释放连接
/// </summary>
public static void DisposeConnection()
{
IDbConnection iConnection = HttpContext.Current.Items[HTTPCONTEXTDB] as IDbConnection;
if (iConnection != null)
{
iConnection.Dispose();
}
}
/// <summary>
/// 新建事务
/// </summary>
public static IDbTransaction BeginTransaction()
{
IDbTransaction transaction = null;
switch (dbType)
{
case DataBaseType.SQLSERVER:
transaction = new DBTransaction(Connection); HttpContext.Current.Items[HTTPCONTEXTDBTransaction] = transaction;
break;
}
return transaction;
}
/// <summary>
/// 得到当前事务
/// </summary>
internal static IDbTransaction Transaction
{
get
{
IDbTransaction transaction = HttpContext.Current.Items[HTTPCONTEXTDBTransaction] as IDbTransaction;
return transaction;
}
}
internal static DbDataAdapter DataAdapter
{
get
{
DbDataAdapter dataAdapter = null;
switch (dbType)
{
case DataBaseType.SQLSERVER:
dataAdapter = new SqlDataAdapter();
break;
}
return dataAdapter;
}
}
internal static DbCommand Command
{
get
{
DbCommand dbCommand = null;
switch (dbType)
{
case DataBaseType.SQLSERVER:
dbCommand = new SqlCommand();
break;
}
return dbCommand;
}
}
}
}
建议换一个微软的petshop范例去研究一下。这个类没有什么亮点,检查代码,看看有多少个switch,这是因为对ADO.NET研究不够才会这么写。
效率一点都不高
效率是不太好,,,那能不能麻烦你大概的将一下那些代码都是些什么意思啊