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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
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 /* •——————————————————————————•
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.gif
{
InBlock.gif    
public class osdData : Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
// Statics
InBlock.gif
        public static string DataLink;
InBlock.gif
InBlock.gif        
// Constructors
InBlock.gif
        static osdData ()
ExpandedSubBlockStart.gif        
{
InBlock.gif            osdData.DataLink 
= ConfigurationManager.AppSettings["OperatorDataLink"];
ExpandedSubBlockEnd.gif        }

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

InBlock.gif                
InBlock.gif        
// Methods
InBlock.gif
        public static DataSet DataSet (string sql, int startindex, int num, string dataname)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.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.gif        
{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf("SERVER"!= -1)
ExpandedSubBlockStart.gif            
{
InBlock.gif                OperatorSql.ExecuteNonQuery(sql);
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static object Executescalar (string SQL, int i)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.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.gif
{
InBlock.gif    
//[DefaultMember("Item")]
InBlock.gif
    public class osdReader
ExpandedSubBlockStart.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.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.gif            
{
InBlock.gif                
this.OleDR = dr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public osdReader (SqlDataReader dr)
ExpandedSubBlockStart.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.gif            
{
InBlock.gif                
this.SqlDR = dr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public osdReader(string SQL)
ExpandedSubBlockStart.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.gif            
{
InBlock.gif                OperatorSql.Open();
InBlock.gif                
this.SqlDR = new SqlCommand(SQL, OperatorSql.ConnSql).ExecuteReader();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.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.gif        
{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.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.gif        
{
InBlock.gif            
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
this.SqlDR.Close();
InBlock.gif                OperatorSql.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.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.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if (osdData.DataLink.ToUpper().IndexOf(";SERVER="!= -1)
ExpandedSubBlockStart.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
 

 

本文转自高阳 51CTO博客,原文链接:http://blog.51cto.com/xiaoyinnet/196545 ,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
开发框架 .NET 数据库
.NETCore 获取数据库上下文[实例的方法和配置连接字符串
.NETCore 获取数据库上下文[实例的方法和配置连接字符串
704 1
|
2月前
|
JavaScript 关系型数据库 MySQL
若依框架数据源切换为pg库
若依管理系统切换数据源为pg数据库
若依框架数据源切换为pg库
|
10月前
|
XML JavaScript API
Qt通过Doc模式读取XML并设计一个增删改查方便的操作类
Qt通过Doc模式读取XML并设计一个增删改查方便的操作类
110 0
|
Go 数据库 数据安全/隐私保护
第四十章 构建数据库应用程序 - 绑定到属性
第四十章 构建数据库应用程序 - 绑定到属性
|
存储 SQL 中间件
第三十七章 构建数据库应用程序 - 在页面上使用对象
第三十七章 构建数据库应用程序 - 在页面上使用对象
|
JavaScript 前端开发
VUE之Elenent-ui之table表格导出、调用后端接口导出(后端返回流文件导出)
VUE之Elenent-ui之table表格导出、调用后端接口导出(后端返回流文件导出)
274 0
|
SQL druid Java
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程
3282 1
Springboot 从数据库读取数据库配置信息,动态切换多数据源 最详细实战教程