当前位置:编程学习 > C#/ASP.NET >>

可移值数据访问层(二)

DBSet.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.Data.SqlClient;


namespace pro
{
    /// <summary>
    /// DBSet 的摘要说明
    /// </summary>
    public class DBSet:DBFactory
    {
        public DBSet()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        /// <summary>
        ///  执行无返回值数据操作,包括insert,update,delete
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>int</returns>
        public static int executeSqlNonQuery(string sql)
        {
            DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), System.Configuration.ConfigurationManager.AppSettings["dbType"]);
            IDbConnection mycon = DBFactory.CreateConnection("testConnectionString", dbType);
            IDbCommand mycmd = DBFactory.CreateCommand(sql,mycon,dbType);
            try
            {
                mycon.Open();
                mycmd.ExecuteNonQuery();
                return 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                mycmd.Dispose();
                mycon.Close();
            }
        }

        /// <summary>
        /// 执行返回一个object对象值的数据操作
        /// </summary>
        /// <param name="sql">sql查询语句</param>
        /// <returns>object</returns>
        public static object executeSqlScalar(string sql)
        {
            DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), System.Configuration.ConfigurationManager.AppSettings["dbType"]);
            IDbConnection mycon = DBFactory.CreateConnection("testConnectionString", dbType);
            IDbCommand mycmd = DBFactory.CreateCommand(sql,mycon,dbType);
            try
            {
                mycon.Open();
                object obj = mycmd.ExecuteScalar();
                return obj;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                mycmd.Dispose();
                mycon.Close();
            }
        }

        /// <summary>
        /// 根据数据库的类型,执行datareader对象
        /// </summary>
        /// <param name="sql"></param>
        //public static void executeDataReader(string sql)
        //{
        //    DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), System.Configuration.ConfigurationManager.AppSettings["dbType"]);
        //    switch (dbType)
        //    {
        //        case DataBaseType.Acess:
        //            executeAceDataReader(sql);
        //            break;
        //        case DataBaseType.Oracle:
        //            executeOracleDataReader(sql);
        //            break;
        //        default:
        //            executeSqlDataReader(sql);
        //            break;
        //    }
        //}

        public static DataSet executeSqlDataSet(string sql)
        {
            DataSet ds = new DataSet();

            DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), System.Configuration.ConfigurationManager.AppSettings["dbType"]);
            IDbConnection mycon = DBFactory.CreateConnection("acessConnectionString", dbType);
            IDbCommand mycmd = DBFactory.CreateCommand(sql, mycon, dbType);
            IDbDataAdapter myida = DBFactory.CreateDataAdapter(mycmd,dbType);
            try
            {
                mycon.Open();
                myida.Fill(ds);
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                mycmd.Dispose();
                mycon.Close();
            }
        }

    }
} --------------------编程问答-------------------- 可移值数据访问层(一)url:http://topic.csdn.net/u/20080512/13/1d468cdd-024a-4a04-8c5e-374c63469940.html?seed=822663832 --------------------编程问答-------------------- 不错
收藏了
指出原创 --------------------编程问答-------------------- 很好很强大,up --------------------编程问答-------------------- 你可以还根据你写的上面那几种方式写,返回相应数据库的DATAREADER就好了:
   ///  <summary> 
        /// 根据数据库的类型,执行datareader对象 
        ///  </summary> 
        ///  <param name="sql"> </param> 
        //public static void executeDataReader(string sql) 
        //{ 
        //    DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType),             System.Configuration.ConfigurationManager.AppSettings["dbType"]); 
IDbCommand mycmd = DBFactory.CreateCommand(sql, mycon, dbType); 

        //    switch (dbType) 
        //    { 
        //        case DataBaseType.Acess: 
        //            AceDataReader ire=mycmd.executeacedatareader(sql);
        //            break; 
        //        case DataBaseType.Oracle: 
        //            oracleDataReader ire=mycmd.executeoracledatareader(sql);
        //            break; 
        //        default: 
        //            SqlDataReader ire=mycmd.executesqlDataReader(sql); 
        //            break; 
        //    } 
        //} 
             return ire;
不知道对不对,我也是菜鸟!呵呵 --------------------编程问答-------------------- 但是他的返回值还是void,我想返回俱体数据库的datareader对象(如返回sqlDataReader,oledbDataReader) --------------------编程问答-------------------- mark,

谢谢 --------------------编程问答-------------------- 楼主是散分吧,呵呵,最好多散点。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,