DotNetSQL是一个轻量级的数据处理封装,建议用作项目数据持久层, 支持 MSSQL2000/2005, 2008下未测试. 可以满足常规的数据处理需要.适合于基于ASP.NET中小型项目!
能够帮助初学者在一定程度上提高开发效率.
DotNetSQL是一个轻量级的数据处理封装,建议用作项目数据持久层, 支持 MSSQL2000/2005, 2008下未测试. 可以满足常规的数据处理需要.适合于基于ASP.NET中小型项目!
能够帮助初学者在一定程度上提高开发效率.
DotNetSQL主要分以下模块
DotNetSQL.Proc :执行常规的存储过程,包括返回datatable,dataset,sqldatareader,return,output
DotNetSQL.Sql:执行常规的sql语句,提取了sqlhelper的基本操作方法.
DotNetSQL.ORM.Sql : 常规的模型操作,insert,update,delete Model
DotNetSQL.ORM.Proc :基于存储过程的模型操作
DotNetSQL.Config:动态配置 (非web.config),适合于winForm
DotNetSQL.Cache:目前只开放了存储过程参数名称缓存,暂无数据缓存.
简单的列出几个调用例子:
一,普通调用存储过程
CREATE PROCEDURE [dbo].[sp_Test]
@a varchar(20),
@b int,
@c float,
@d text,
@e datetime,
@f int output,
@g varchar(20) output
AS
BEGIN
SET NOCOUNT ON;
--逻辑处理代码![](https://www.cnblogs.com/Images/dot.gif)
END
这是一个简单的存贮过程,里面关系到了输入参数,输出参数,用DotNetSQL执行代码如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using DotNetSQL.Proc;
ExecuteProc ep = new ExecuteProc();
Dictionary<string, object> Out;
int f;
string g;
Out=ep.RunProc("sp_Test","LiLei",20,32.3,"这里是长文本",DateTime.Now);
//参数说明
//RunProc(存储过程名称,参数a,参数b,参数c,参数d,参数e) 无需再每个参数都new一次SqlParameter对象
//读取output参数
if(Out.ContainsKey("@f")){
f=(int)Out["@f"];
}
if(Out.ContainsKey("@g")){
g=Out["@g"].ToString();
}
此外 ExecuteProc下还有多个方法,例如无任何返回值,返回DataTable,SqlDataReader,DataSet等等.
另外备注一下使用DotNetSQL执行存储过程与常规ADO.NET操作的性能对比.
![](https://images.cnblogs.com/cnblogs_com/hcl0208/未命名-1.jpg)
以上测试是在我的小本本上测试结果,两者误差基本在2ms内.
二,基于ORM的数据操作
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
C# Code(Model类)
using System;
using DotNetSQL.ORM; //必需引用该命名空间
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Property("Member")] //设置该model对应的表名
public class Member
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public Member()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
//TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _id;
private string _name;
private bool _sex;
private decimal _money;
private DateTime _addtime;
private string _ext;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property(ColumnKeyType.PrimaryKeyAndIdentity)] //表示Id列为该表的主键且自增
public int Id
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Sex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _sex; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _sex = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property(ColumnKeyType.ReadOnly)] //表示在处理数据时,该列为只读,即不参与修改.
public decimal Money
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _money; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _money = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DateTime AddTime
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _addtime; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _addtime = value; }
}
[Property(ColumnKeyType.Extend)] //表示该列为扩展列,当数据库表中不存在该字段时,可以标识该属性
public string Ext
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ext; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _ext = value; }
}
}
//ColumnKeyType详细枚举请参考文档
注:Model类中的字段必需于数据库中表的字段名称相同,包括大小写,建议使用代码生成器生成Model!
DotNetSQL的操作代码如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
Member memberModel = new Member();
ORMToSQL ormToSql = new ORMToSQL();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//根据主键(Id)得到实体。
memberModel=ormToSql.GetModel<Member>(1); //得到Id=1的数据行(实体)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
if(memberModel!=null)
{
Response.Write (memberModel.Name);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//插入一个新的实体到数据库中
memberModel.Name="sun.Lei";
memberModel.Sex=false;
memberModel.Money=45.46; //因为在model类中,给该列标识了ReadOnly属性,所以不会插入该列值
memberModel.AddTime = DateTime.Now;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int key=ormToSql.InsertModel<Member>(memberModel); //插入数据,并返回该条数据的自增ID
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//update,delete都类似,此外以上方法都有相关重载,例如更新指定的列,详情参照文档。
DotNetSQL还集成了基于存储过程的模型操作。
DotNetSQL也集成了sqlhelper中的基本操作方法。
具体请参见文档,点击下载
欢迎大家批评指正,如果你对DotNetSQL感兴趣可以向我索要源码!也希望DotNetSQL能够帮助初学者实现快速开发!