欢迎访问我的博客 https://javascript.shop

可移植的数据库

原文发布时间为:2008-08-02 —— 来源于本人的百度文章 [由搬家工具导入]

web.config里面:

<?xml version="1.0"?>
<configuration>
<appSettings>
    <add key="dbType" value="Access"/>
    <!--这里可选值必须是 SqlServer 或者 Access 再或者 Oracle-->
</appSettings>
<connectionStrings>
   <add name="tempConn" connectionString="Data Source=thc\sqlexpress;Initial Catalog=temp;Persist Security Info=True;User ID=thc123_com;Password=thc123_net" providerName="System.Data.SqlClient"/>
   <add name="nameConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\name.mdb;Persist Security Info=True" providerName="System.Data.OleDb"/>
</connectionStrings>

--------------------------------------------------------

fdatatype类

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;
//using System.Data.OracleClient; //我机器上没装Oracle,无法给大家演示,但是原理是一样就做出来了

/// <summary>
/// 向应用程序的较高级别隐藏与创建特定类型(来自特定的数据提供程序)的实例有关的细节,应用程序现在可以使用通过基本接口公开的一般行为与数据源进行交互。
/// 天轰穿 www.thc123.com
/// </summary>

namespace SAO
{//定义一个命名空间
    /// <summary>
    /// 移植到的数据库名称集
    /// </summary>
    public enum DataBaseType
    {//申明一个枚举类型的,枚举参数为我们可能会移植到的数据库名称集
        SqlServer,
        Access,
        Oracle
        //。。。其他可能用到的数据库名称
    }
    /// <summary>
    /// 公用参数的类型集
    /// </summary>
    public enum ParameterType
    {//定义一个枚举类型的公用参数的类型集
        Integer,
        Char,
        VarChar
    }
    public class fdatatype
    {
        public fdatatype()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 创建并初始化Connection连接实例
        /// </summary>
        /// <param name="dbType">数据提供程序类型</param>
        /// <returns>返回一个已经创建好的数据库连接实例</returns>
        public static IDbConnection CreateConnection(DataBaseType dbType)
        {
            IDbConnection con; //申明一个IDbConnection实例
            switch(dbType)
            {//根据枚举值来判断创建啥类型的数据库连接实例
                case DataBaseType.Access:
                    con = new OleDbConnection(ConfigurationManager.ConnectionStrings["nameConn"].ConnectionString);
                    break;
                //case DataBaseType.Oracle:
                //    con = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
                //    break;
                default:
                    con = new SqlConnection(ConfigurationManager.ConnectionStrings["tempConn"].ConnectionString);
                    break;
            }
            return con;
        }
        /// <summary>
        /// 创建并初始化Command实例
        /// </summary>
        /// <param name="QueryString">Sql查询语句或存储过程名称</param>
        /// <param name="dbType">数据提供程序类型</param>
        /// <param name="con">数据库连接实例</param>
        /// <returns>返回一个已经创建好的Command实例对象</returns>
        public static IDbCommand CreateCommand(string QueryString, DataBaseType dbType,IDbConnection con)
        {
            IDbCommand cmd;
            switch(dbType)
            {
                case DataBaseType.Access:
                    cmd = new OleDbCommand(QueryString, (OleDbConnection)con);
                    //注意到(OleDbConnection)con),这一段代表讲参数中的数据库连接实例强制类型转换为XXX类型
                    break;
                //case DataBaseType.Oracle:
                //    cmd = new OracleCommand(QueryString, (OracleConnection)con);
                //    break;
                default:
                    cmd = new SqlCommand(QueryString, (SqlConnection)con);
                    break;
            }
            return cmd;
        }
        /// <summary>
        /// 创建并初始化一个DataAdapter对象
        /// </summary>
        /// <param name="cmd">Command实例对象</param>
        /// <param name="dbType">数据提供程序类型</param>
        /// <returns>返回一个已经创建好的DataAdapter对象</returns>
        public static IDbDataAdapter CreateDataAdapter(IDbCommand cmd, DataBaseType dbType)
        {
            IDbDataAdapter ida;
            switch (dbType)
            {
                case DataBaseType.Access:
                    ida = new OleDbDataAdapter((OleDbCommand)cmd);
                    break;
                //case DataBaseType.Oracle:
                //    ida = new OracleDataAdapter((OracleCommand)cmd);
                //    break;
                default:
                    ida=new SqlDataAdapter((SqlCommand)cmd);
                    break;
            }
            return ida;
        }
    }
}
-----------------------------------------

fdataset类

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;

namespace SAO
{
    /// <summary>
    /// 使用我们前面创建的哪个类来与数据库建立连接并获取数据
    /// </summary>
    public class fdataset
    {
        DataBaseType dbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), ConfigurationSettings.AppSettings["dbType"]);
        IDbConnection mycon;
        DataSet ds;

        public fdataset()
        {
            mycon = fdatatype.CreateConnection(dbType);
            ds = new DataSet();
        }
       
        //得到我们在web.config文件中设置的数据库提供程序
        public DataSet Tds()
        {
            IDbCommand mycmd = fdatatype.CreateCommand("select * from name", dbType, mycon);
            IDbDataAdapter myda = fdatatype.CreateDataAdapter(mycmd, dbType);
            myda.Fill(ds);
            return ds;
        }
    }
}

posted @ 2017-07-10 00:48  孑孓子  阅读(180)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop