Cocon90.DB 使用说明

开源库:https://github.com/Cocon90/Cocon90.Db

Sqlite位置:https://www.nuget.org/packages/Cocon90.Db.Sqlite

Mysql位置:https://www.nuget.org/packages/Cocon90.Db.Mysql

SqlServer位置:https://www.nuget.org/packages/Cocon90.Db.SqlServer

一、简介
Cocon90.Db是由Cocon90.Db.Common为核心的类库与其它数据库操作库组合而成,以方便调用为主要目的,支持ORM操作,增、删、改、查、事务、批量执行、创建表、插入或保存记录 等等,并提供多种数据库支持。当前已支持Mysql、Sqlite、SqlServer。

Cocon90.Db is a core class library which is composed of Cocon90.Db.Common and other database operations, which is convenient to call for the main purpose, and provides a variety of database support. Currently supports Mysql, Sqlite, SqlServer.

二、运行环境

环境
NetFramework4.0+ Required
VisualStudio 2015+ Required

三、使用方法
(1)新建一件空的项目,取名Cocon90.Db.Demo
(2)添加对库Cocon90.Db.Common.dll的引用,或者执行Install-Package Cocon90.Db.Common
(3)接下来,如果要操作SqlServer数据库则引入Cocon90.Db.SqlServer,如果要操作Sqlite数据库,则引入Cocon90.Db.Sqlite,如果要操作Mysql,则需要引入Cocon90.Db.Mysql库。

DataBase Require Library
MySql Cocon90.Db.Common,Cocon90.Db.Mysql
Sqlite Cocon90.Db.Common,Cocon90.Db.Sqlite
SqlServer Cocon90.Db.Common,Cocon90.Db.SqlServer

(4)新建Program类,加入Main函数,比如我们要操作Sqlite数据库加入下列代码:

static void Main(string[] args)
{
var dh = Cocon90.Db.Common.Db.GetDataHelper("Cocon90.Db.Sqlite.dll", "Cocon90.Db.Sqlite.DbDriver", "D:\\mysqlite.db;");
// or you can use app.config by this code:
var dh = Cocon90.Db.Common.Db.GetDataHelper();
}

如果在要读取配置文件中的app.config中的连接语句,则需要在App.config中进行如下配置:

<configuration>
<connectionStrings>
<!--<add name="ConnectionString" providerName="Cocon90.Db.Mysql.dll|Cocon90.Db.Mysql.DbDriver" connectionString="Server=127.0.0.1;Port=3306;Database=world;Uid=root;Pwd=123456;"/>-->
<!--<add name="ConnectionString" providerName="Cocon90.Db.SqlServer.dll|Cocon90.Db.SqlServer.DbDriver" connectionString="Server=127.0.0.1;Database=world;Uid=sa;Pwd=123456;"/>-->
<add name="ConnectionString" providerName="Cocon90.Db.Sqlite.dll|Cocon90.Db.Sqlite.DbDriver" connectionString="Data Source=D:\Application\DbTools\sqliteSpy\SQLiteSpy.db3;"/>
</connectionStrings>
</configuration>

然后即可进行数据库的常规操作:

    var tab = dh.GetTable("select * from Student");
dh.ExecNoQuery("...");
dh.GetString("...");
dh.GetBoolean("...");
dh.GetInt("...");
dh.GetListString("...");
dh.GetPagedResult("...");
dh.CreateOrUpdateTable(typeof(Model.Student));

下面是常用的一些测试:

 class Program
{
static void Main(string[] args)
{
//var dh = Cocon90.Db.Common.Db.GetDataHelper("Cocon90.Db.Sqlite.dll", "Cocon90.Db.Sqlite.DbDriver", "D:\\Application\\DbTools\\sqliteSpy\\SQLiteSpy.db3;"); var dh = Cocon90.Db.Common.Db.GetDataHelper(); var createSql = dh.GetCreateTableSql<Model.CountryLanguageModel>();
var updateTabSql = dh.GetUpdateTableSql(typeof(Model.CountryLanguageModel));
var effRow = dh.CreateOrUpdateTable<Model.CountryLanguageModel>(); var needInserts = new List<Model.CountryLanguageModel>();
Random rand = new Random();
for (int i = 0; i < 500; i++)
{
needInserts.Add(new Model.CountryLanguageModel() { Percent = (decimal)(rand.NextDouble() * 10), Date = DateTime.Now.AddDays(-1 * i), Guid = Guid.NewGuid(), IsOfficial = rand.Next(0, 2) > 0, Code = i, Language = "Lang_" + i });
}
var succRows = dh.Save(needInserts.ToArray());
dh.GetTable("SELECT * FROM countrylanguage");
var lst = dh.GetList<Model.CountryLanguageModel>("SELECT * FROM countrylanguage");
Console.WriteLine(lst.Count);
var oneModel = dh.GetOne<Model.CountryLanguageModel>("select * from countrylanguage");
var oneModel2 = dh.GetOneByPrimaryKey<Model.CountryLanguageModel>(1, "Lang_1");
//var successRows = dh.Insert(new Model.CountryLanguage() { Percent = 1.555m, IsOfficial = false, Code = 2, Language = "Lang" },
// new Model.CountryLanguage() { Percent = 1.66m, IsOfficial = true, Code = 3, Language = "Lang" });
var updateSql = dh.GetUpdateSqlByPrimaryKey(new Model.CountryLanguageModel() { Percent = 9.9m }, true, "1=1 AND 2=2", 3, "Lang");
var updateSql2 = dh.GetUpdateSql(new Model.CountryLanguageModel { Code = 3, Percent = 3.3m }, false, null);
var updateSql3 = dh.GetUpdateSqlByWhere(new Model.CountryLanguageModel { Code = 3, Percent = 3.3m }, true, "Language='Lang'", new Common.Data.Params("@Name", "song"));
var updateRow3 = dh.UpdateByByWhere(new Model.CountryLanguageModel { Percent = 3.3m }, true, "Language='Lang'");
var updateRow = dh.UpdateByPrimaryKey(new Model.CountryLanguageModel { Percent = 4.5m }, true, null, 3, "Lang");
var deleteSql = dh.GetDeleteSqlByPrimaryKey<Model.CountryLanguageModel>("1=1", 3, "Lang");
var deleteSql1 = dh.GetDeleteSqlByPrimaryKey<Model.CountryLanguageModel>(null, 3, "Lang_111");
var deleteSql2 = dh.GetDeleteSqlByWhere<Model.CountryLanguageModel>("Percentage=@Perc", new Common.Data.Params("Perc", 100));
var deleteSql3 = dh.GetDeleteSql(new Model.CountryLanguageModel { Code = 3, Percent = 3.3m }, "1=@myParam", new Common.Data.Params("myParam", 1));
var deleteSql4 = dh.GetDeleteSql<Model.CountryLanguageModel>(null, "1=@myParam", new Common.Data.Params("myParam", 1));
var successRow = dh.Delete(new Model.CountryLanguageModel { Code = 3, Percent = 4.5m }); var saveSql = dh.GetSaveSql(new Model.CountryLanguageModel() { Percent = 1.555m, IsOfficial = false, Code = 2, Language = "Lang" },
new Model.CountryLanguageModel() { Percent = 1.66m, IsOfficial = true, Code = 3, Language = "Lang" });
var saveRows = dh.Save(new Model.CountryLanguageModel() { Percent = 1.555m, IsOfficial = false, Code = 2, Language = "Lang" },
new Model.CountryLanguageModel() { Percent = 1.66m, IsOfficial = true, Code = 3, Language = "Lang" });
var executeNoQuery = dh.ExecNoQuery("update countrylanguage set Percentage=4.4 where Percentage=@Percentage", new Model.CountryLanguageModel { Percent = 1.6m });
var pageSql = dh.Driver.GetPagedSql("select * from countrylanguage", "CountryCode", true, 1, 10);
var pageResult = dh.GetPagedResult<Model.CountryLanguageModel>("select * from countrylanguage", "countrycode", true, 1, 10); }
}

其中测试实体类:

    public class Student
{
[Cocon90.Db.Common.Attribute.Column(PrimaryKey = true)]
public Guid? Id { get; set; }
public string Name { get; set; }
public string Addrss { get; set; }
public DateTime? Birthday { get; set; }
}
    [Table(TableName = "CountryLanguage")]
public class CountryLanguageTab
{
[Column(PrimaryKey = true, ColumnName = "CountryCode")]
public int? Code { get; set; }
[Column(PrimaryKey = true, CreateDDL = "varchar(20)")]
public string Language { get; set; }
public bool? IsOfficial { get; set; }
[Column(ColumnName = "Percentage", PrimaryKey = false)]
public decimal? Percent { get; set; }
public DateTime? Date { get; set; }
public Guid? Guid { get; set; }
} [Table(TableName = "CountryLanguage")]
public class CountryLanguageModel : CountryLanguageTab
{
[Ignore]
public string CodeAndLang { get; set; }
}

需要注意的是,实体的设计时,所有类型都必须可以为NULL值(如果是结构体类型,请采用可空类型)。

Cocon90.Db调用方法的更多相关文章

  1. 完整的分页存储过程以及c#调用方法

    高效分页存储过程 USE [db] GO /****** 对象: StoredProcedure [dbo].[p_Page2005] 脚本日期: // :: ******/ SET ANSI_NUL ...

  2. xpath爬取网页评论,网址的的调用方法,数据库特殊字符的替换

    # -*- coding:utf-8-*-from lxml import etreeimport urllibimport jsonimport requestsimport MySQLdbid=0 ...

  3. phpcms 的实用相关接口,函数,调用方法

    常用函数 , 打开include/global.func.php,下面存放一些公共函数view plaincopy to clipboardprint? strip_tags() 调用内容过滤html ...

  4. phpcms常用接口调用方法

    常用函数 , 打开include/global.func.php,下面存放一些公共函数 view plaincopy to clipboardprint?function str_charset($i ...

  5. 关于AOP无法切入同类调用方法的问题

    一.前言 Spring AOP在使用过程中需要注意一些问题,也就是平时我们说的陷阱,这些陷阱的出现是由于Spring AOP的实现方式造成的.每一样技术都或多或少有它的局限性,很难称得上完美,只要掌握 ...

  6. [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  7. ThinkPHP跨控制器调用方法

    跨控制器调用方法 1. 先造对象,再调用里面的方法 $sc=new \Home\Controller\IndexController();  用绝对路径找echo $sc->ShuChu(); ...

  8. C# 反射之调用方法谈

    反射的定义 反射提供了描述程序集.模块和类型的对象(Type 类型). 可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性. 如果代码中使用了特性 ...

  9. 利用反射调用方法时,处理ref,out参数需要注意的问题(转)

    转自:http://www.68idc.cn/help/buildlang/ask/20150318283817.html 项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现 ...

随机推荐

  1. arcgispro字段计算器

    使用python语法 在python中没有类似sub()或者subString()的方法,但是字符串的截取操作却是更加简单. 只需要把字符串看作是一个字符数组,截取子串非常方便. 多余的话就不啰嗦了, ...

  2. 禁止浏览器backspace键(退格键)时跳转页面(extjs,javascript)

    Ext实现方式: //方法一  var key = new Ext.KeyMap(document,{   key: 8,   fn: function(obj,e){    var type = e ...

  3. Select、Poll与Epoll比較

    (1)select select最早于1983年出如今4.2BSD中,它通过一个select()系统调用来监视多个文件描写叙述符的数组.当select()返回后,该数组中就绪的文件描写叙述符便会被内核 ...

  4. 分享到微信、微博、QQ空间、QQ微博

    一:分享到微信 //分享到微信$("#weixin").bind("click", function () {    var p = {        url: ...

  5. 如何使用LaTeX让自己不乱?

    虽然说LaTeX声称排版容易,只关注内容,可是混合着源代码的结构很难让我只关注内容,最后看得眼睛疼,找什么都找不到. 匿名用户 30 人赞同 立即想到的几个建议: 选择有折叠功能 (folding) ...

  6. 从阿里Java开发手册学习线程池的正确创建方法

    前言 最近看阿里的 Java开发手册,上面有线程池的一个建议: [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更 ...

  7. MongoDB 分布式部署教程

    本文将介绍如何使用 MongoDB 提供的 Replica Set 和 Shards 功能构建一个分布式 MongoDB 集群. Replica Set 部署 我们先从部署一个三节点的 Replica ...

  8. Django静态文件的加载以及STATIC_URL、 STATIC_ROOT 、STATICFILES_DIRS的区别

    Djangon生产环境静态资源的处理 Django 关闭DEBUG模式后,就相当于是生产环境了. Django框架一旦作为生产环境,它的静态文件访问接口就不应该从Django框架中走,必须在Djang ...

  9. 【BLE】CC2541之发现服务与特征值

    一.简介 本文以SimpleBLECentral工程为例,解析CC2541作为主机时是如何发现从机的服务和特征值的. 二.实验平台 协议栈版本:BLE-CC254x-1.4.0 编译软件:IAR 8. ...

  10. knockout 多值绑定

    knockout 这种东西现在已经很流行了,相信很多人对它的使用都已经很熟悉了,最近项目开发中发现knockout 绑定用的有些不怎么充分,发现整个page的code 有点累赘. 1.在绑定click ...