Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)

WebConf配置:

<?xml version="1.0"?>
<!--

    注意: 除了手动编辑此文件以外,您还可以使用

    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。

    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
 <appSettings>
  <!--***************** 数据库的设置 *****************
  !— OperatorDataCode:  数据库操作引擎授权码(自动判断域名)
  !— OperatorDataLink:  数据库链接参数(自动判断类型)
  !— Access类型:     /安装目录/Data/数据库名.mdb
  !— SQL Server类型: uid=账号;pwd=密码;database=数据库;server=服务器
 **************************************************-->
  <add key="OperatorDataCode"  value="00000000-00000000"/>
  <!add key="OperatorDataLink"  value="dbAcc.mdb"/>
        <!--add key="OperatorDataLink"  value="uid=sa;pwd=sa;database=dbSQL;server=."/-->
  <!--***********************************************-->
 </appSettings>
 <connectionStrings/>
 <system.web>
  <!--

            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。

        -->
  <compilation debug="true"/>
  <!--

            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。

        -->

  <authentication mode="Windows"/>
       
        <customErrors mode="RemoteOnly"/>
  <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。
 
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
 </system.web>
</configuration>
ExpandedBlockStart.gif ContractedBlock.gif /**/ /* •——————————————————————————•
InBlock.gif   | Title:  智能判断数据源                            |
InBlock.gif   | Project: DBOperatorService.Data                   |
InBlock.gif   | Subarea: DataSet                                  |
InBlock.gif   | Author: ξ箫音ξ                                  |
InBlock.gif   | Website: www.crfly.com;bbs.52happy.net            |
InBlock.gif   | Created date: 01/16/2007                          |
InBlock.gif   | Changed date: 01/17/2007                          |
ExpandedBlockEnd.gif   •——————————————————————————• 
*/

None.gif
None.gif
using  System;
None.gif
using  System.Configuration;
None.gif
using  System.Web.UI;
None.gif
using  DBOperator.Data;
None.gif
None.gif
namespace  System.Data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class osdData : Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Statics
InBlock.gif
        public static string DataLink;
InBlock.gif
InBlock.gif        
// Constructors
InBlock.gif
        static osdData ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            osdData.DataLink 
= ConfigurationManager.AppSettings["OperatorDataLink"];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public osdData ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif                
InBlock.gif        
// Methods
InBlock.gif
        public static DataSet DataSet (string sql, int startindex, int num, string dataname)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return OperatorSql.ExecuteDataSet(sql, startindex, num, dataname);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return OperatorAcc.ExecuteDataSet(sql, startindex, num, dataname);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static void ExecuteNonQuery (string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf("SERVER"!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OperatorSql.ExecuteNonQuery(sql);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OperatorAcc.ExecuteNonQuery(sql);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static object Executescalar (string SQL, int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return OperatorSql.ExecuteScalar(SQL, i);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return OperatorAcc.ExecuteScalar(SQL, i);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Reflection;
None.gif
using  DBOperator.Data;
None.gif
None.gif
namespace  System.Data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//[DefaultMember("Item")]
InBlock.gif
    public class osdReader
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Instance Fields
InBlock.gif
        public osdData myData;
InBlock.gif        
public OperatorAcc myAcc;
InBlock.gif        
public OperatorSql mySql;
InBlock.gif        
private OleDbDataReader OleDR;
InBlock.gif        
private SqlDataReader SqlDR;
InBlock.gif
InBlock.gif        
// Constructors
InBlock.gif
        public osdReader (OleDbDataReader dr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myData = new osdData();
InBlock.gif            
this.myAcc = new OperatorAcc();
InBlock.gif            
this.mySql = new OperatorSql();
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="== -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OleDR = dr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public osdReader (SqlDataReader dr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myData = new osdData();
InBlock.gif            
this.myAcc = new OperatorAcc();
InBlock.gif            
this.mySql = new OperatorSql();
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SqlDR = dr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public osdReader(string SQL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myData = new osdData();
InBlock.gif            
this.myAcc = new OperatorAcc();
InBlock.gif            
this.mySql = new OperatorSql();
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OperatorSql.Open();
InBlock.gif                
this.SqlDR = new SqlCommand(SQL, OperatorSql.ConnSql).ExecuteReader();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OperatorAcc.Open();
InBlock.gif                
this.OleDR = new OleDbCommand(SQL, OperatorAcc.ConnAcc).ExecuteReader();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        
// Methods
InBlock.gif
        public bool Read ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.SqlDR.Read();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return this.OleDR.Read();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void Close ()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SqlDR.Close();
InBlock.gif                OperatorSql.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OleDR.Close();
InBlock.gif                OperatorAcc.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        
// Properties
InBlock.gif
        public object this[string cs]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return this.SqlDR[cs];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return this.OleDR[cs];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


使用方法简单说明:

1、代码对比
 
1)传统Web网站数据库编程代码
 
GridView1.DataSource = ds.Tables[TableName1].DefaultView;
GridView1.DataBind();
 
2)使用DBOperator.Data数据库组件
        GridView1.DataSource = ds;
        GridView1.DataBind();
 
2、数据库配置

WebConfig里使用哪个数据库,就打开哪个。

 <!add key="OperatorDataLink"  value="dbAcc.mdb"/>
        <!--add key="OperatorDataLink"  value="uid=sa;pwd=sa;database=dbSQL;server=."/-->
 
3、数据源调用

例如:1) DataSet调用方法:
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = osdData.DataSet("SELECT * FROM XiaoYin_User", 0, 0, "dsTable");//使用组
       件的scData类,实现DataSet功能
        //数据源
        GridView1.DataSource = ds;
        //为GridView绑定数据
        GridView1.DataBind();
    }
      2) Reader调用方法:
      protected void Page_Load(object sender, EventArgs e)
      {
        //使用scReader类,实现DataReader功能
        osdReader dr = new osdReader("SELECT * FROM XiaoYin_User");
        //循环启动阅读器
        while (dr.Read())
        {
            //输出指定列
            Response.Write(dr["u_name"] + "<br>");
        }
        dr.Close();//关闭阅读器
      }

实现功能:
     
osdDataSet类
     读取(DataSet方式),插入,更新,删除,统计
     1,读取
       DataSet ds=osdData.DataSet("SELECT * FROM 表 WHERE 条件",开始行,多少行,"虚拟表名");
     2,插入
       osdData.ExecuteNonQuery("INSERT INTO 表 (列1,列2) VALUES (变量1,变量2)");
     3,更新
       osdData.ExecuteNonQuery("UPDATE 表 SET 列1=变量A,列2=变量B WHERE 条件");
     4,删除
       osdData.ExecuteNonQuery("DELETE 表 WHERE 条件");
     5,统计
       osdData.ExecuteScalar("SELECT * FROM 表 WHERE 条件",统计类型)
统计类型分两种:

       int 整型:1
       double 带小数点:2
//---------- osdReader类 --------------------
实现功能:读取(DataReader阅读器方式)
调用方法:
osdReader dr=new osdReader("SELECT * FROM 表 WHERE 条件");
//--------------------------------------------
if(dr.Read())
{
   //如果特定条件的值存在,立即终止下一行的读取
}
//--------------------------------------------
while(dr.Read)
{
   //循环读取符合条件的值
}
//--------------------------------------------
调用读取出来的值:
dr["列名"].ToString();
使用完后关闭:
dr.Close();

全部源码与实例打包下载地址:

http://bbs.crfly.com/19591/ShowPost.aspx
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值