WebConf配置:
/* •——————————————————————————•
| Title: 智能判断数据源 |
| Project: DBOperatorService.Data |
| Subarea: DataSet |
| Author: ξ箫音ξ |
| Website: www.crfly.com;bbs.52happy.net |
| Created date: 01/16/2007 |
| Changed date: 01/17/2007 |
•——————————————————————————• */
using System;
using System.Configuration;
using System.Web.UI;
using DBOperator.Data;
namespace System.Data
{
public class osdData : Page
{
// Statics
public static string DataLink;
// Constructors
static osdData ()
{
osdData.DataLink = ConfigurationManager.AppSettings["OperatorDataLink"];
}
public osdData ()
{
}
// Methods
public static DataSet DataSet (string sql, int startindex, int num, string dataname)
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return OperatorSql.ExecuteDataSet(sql, startindex, num, dataname);
}
return OperatorAcc.ExecuteDataSet(sql, startindex, num, dataname);
}
public static void ExecuteNonQuery (string sql)
{
if (osdData.DataLink.ToUpper().IndexOf("SERVER") != -1)
{
OperatorSql.ExecuteNonQuery(sql);
}
else
{
OperatorAcc.ExecuteNonQuery(sql);
}
}
public static object Executescalar (string SQL, int i)
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return OperatorSql.ExecuteScalar(SQL, i);
}
return OperatorAcc.ExecuteScalar(SQL, i);
}
}
}
using System;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Reflection;
using DBOperator.Data;
namespace System.Data
{
//[DefaultMember("Item")]
public class osdReader
{
// Instance Fields
public osdData myData;
public OperatorAcc myAcc;
public OperatorSql mySql;
private OleDbDataReader OleDR;
private SqlDataReader SqlDR;
// Constructors
public osdReader (OleDbDataReader dr)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") == -1)
{
this.OleDR = dr;
}
}
public osdReader (SqlDataReader dr)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
this.SqlDR = dr;
}
}
public osdReader(string SQL)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
OperatorSql.Open();
this.SqlDR = new SqlCommand(SQL, OperatorSql.ConnSql).ExecuteReader();
}
else
{
OperatorAcc.Open();
this.OleDR = new OleDbCommand(SQL, OperatorAcc.ConnAcc).ExecuteReader();
}
}
// Methods
public bool Read ()
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return this.SqlDR.Read();
}
return this.OleDR.Read();
}
public void Close ()
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
this.SqlDR.Close();
OperatorSql.Close();
}
else
{
this.OleDR.Close();
OperatorAcc.Close();
}
}
// Properties
public object this[string cs]
{
get
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return this.SqlDR[cs];
}
return this.OleDR[cs];
}
}
}
}
| Title: 智能判断数据源 |
| Project: DBOperatorService.Data |
| Subarea: DataSet |
| Author: ξ箫音ξ |
| Website: www.crfly.com;bbs.52happy.net |
| Created date: 01/16/2007 |
| Changed date: 01/17/2007 |
•——————————————————————————• */
using System;
using System.Configuration;
using System.Web.UI;
using DBOperator.Data;
namespace System.Data
{
public class osdData : Page
{
// Statics
public static string DataLink;
// Constructors
static osdData ()
{
osdData.DataLink = ConfigurationManager.AppSettings["OperatorDataLink"];
}
public osdData ()
{
}
// Methods
public static DataSet DataSet (string sql, int startindex, int num, string dataname)
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return OperatorSql.ExecuteDataSet(sql, startindex, num, dataname);
}
return OperatorAcc.ExecuteDataSet(sql, startindex, num, dataname);
}
public static void ExecuteNonQuery (string sql)
{
if (osdData.DataLink.ToUpper().IndexOf("SERVER") != -1)
{
OperatorSql.ExecuteNonQuery(sql);
}
else
{
OperatorAcc.ExecuteNonQuery(sql);
}
}
public static object Executescalar (string SQL, int i)
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return OperatorSql.ExecuteScalar(SQL, i);
}
return OperatorAcc.ExecuteScalar(SQL, i);
}
}
}
using System;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Reflection;
using DBOperator.Data;
namespace System.Data
{
//[DefaultMember("Item")]
public class osdReader
{
// Instance Fields
public osdData myData;
public OperatorAcc myAcc;
public OperatorSql mySql;
private OleDbDataReader OleDR;
private SqlDataReader SqlDR;
// Constructors
public osdReader (OleDbDataReader dr)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") == -1)
{
this.OleDR = dr;
}
}
public osdReader (SqlDataReader dr)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
this.SqlDR = dr;
}
}
public osdReader(string SQL)
{
this.myData = new osdData();
this.myAcc = new OperatorAcc();
this.mySql = new OperatorSql();
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
OperatorSql.Open();
this.SqlDR = new SqlCommand(SQL, OperatorSql.ConnSql).ExecuteReader();
}
else
{
OperatorAcc.Open();
this.OleDR = new OleDbCommand(SQL, OperatorAcc.ConnAcc).ExecuteReader();
}
}
// Methods
public bool Read ()
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return this.SqlDR.Read();
}
return this.OleDR.Read();
}
public void Close ()
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
this.SqlDR.Close();
OperatorSql.Close();
}
else
{
this.OleDR.Close();
OperatorAcc.Close();
}
}
// Properties
public object this[string cs]
{
get
{
if (osdData.DataLink.ToUpper().IndexOf(";SERVER=") != -1)
{
return this.SqlDR[cs];
}
return this.OleDR[cs];
}
}
}
}
使用方法简单说明:
1、代码对比
1)传统Web网站数据库编程代码
GridView1.DataSource = ds.Tables[TableName1].DefaultView;
GridView1.DataBind();
GridView1.DataBind();
2)使用DBOperator.Data数据库组件
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.DataBind();
2、数据库配置
WebConfig里使用哪个数据库,就打开哪个。
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;
GridView1.DataSource = ds;
//为GridView绑定数据
GridView1.DataBind();
}
GridView1.DataBind();
}
2) Reader调用方法:
protected void Page_Load(object sender, EventArgs e)
{
//使用scReader类,实现DataReader功能
osdReader dr = new osdReader("SELECT * FROM XiaoYin_User");
{
//使用scReader类,实现DataReader功能
osdReader dr = new osdReader("SELECT * FROM XiaoYin_User");
//循环启动阅读器
while (dr.Read())
{
//输出指定列
Response.Write(dr["u_name"] + "
");
}
dr.Close();//关闭阅读器
}
while (dr.Read())
{
//输出指定列
Response.Write(dr["u_name"] + "
");
}
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
实现功能:读取(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