首页 新闻 搜索 专区 学院

ASP.NET 数据库工厂 求详解。。。

0
悬赏园豆:10 [待解决问题]

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;            

}        

}

    }

}

鸣飞的主页 鸣飞 | 初学一级 | 园豆:183
提问于:2013-04-19 14:51
< >
分享
所有回答(2)
0

建议换一个微软的petshop范例去研究一下。这个类没有什么亮点,检查代码,看看有多少个switch,这是因为对ADO.NET研究不够才会这么写。

呆呆蚁 | 园豆:231 (菜鸟二级) | 2013-04-19 15:08
0

效率一点都不高

不负春光,努力生长 | 园豆:1382 (小虾三级) | 2013-04-19 21:22

效率是不太好,,,那能不能麻烦你大概的将一下那些代码都是些什么意思啊

支持(0) 反对(0) 鸣飞 | 园豆:183 (初学一级) | 2013-04-19 21:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册