.NET平台上有用的DLL

1. Dapper 数据存取框架

官方网站
https://stackexchange.github.io/Dapper/
http://blog.youkuaiyun.com/laokang426/article/details/77885137
ORM的概念, ORM到底是什么

Dapper是一个轻量级的ORM工具:ORM框架的核心思想是对象关系映射,ORM是将表与表之间的操作,映射成对象和对象之间的操作,就是通过操作实体类来达到操作表的目的。从数据库提取的数据会自动按你设置的映射要求封装成特定的对象。之后你就可以通过对对象进行操作来修改数据库中的数据。这时候你面对的不是信息的碎片,而是一个形象鲜明的对象。
ORM 框架很多: Dapper、 Mybatis.Net、EntityFramework 和 NHibernate。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。而Mybatis.Net需要配置XML文件,综合考虑你会觉得觉得ORM省时省力。


支持多数据库的本质是因为Dapper是对IDBConnection接口进行了方法扩展,代码就一个SqlMapper.cs文件,编译后就40K的一个很小的,DllSqlConnection,MysqlConnection,OracleConnection都是继承于DBConnection,而DBConnection又是实现了IDBConnection的接口
创建一个数据库表对应的类

项目中的使用方法:
https://www.jianshu.com/p/c4ca2989d26a

  1. 对数据库中的每个表建立一个类;
  2. 写一个对数据库的操作接口类,把所有对数据库的操作封装起来;

    一般查询到的结果是IEnumerable集合类型,想要从中取出单一的元素,可以使用Single、First、Last、ElementAt等方法,以及它们带有OrDefault的形式。Single返回序列中的唯一元素,First、Last返回第一个、最后一个元素。
    https://www.cnblogs.com/MrEggplant/p/5394593.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.Model
{
    public class Book
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public double Price { get; set; }
        public string Press { get; set; }
        public DateTime ReleaseTime { get; set; }

        public Book()
        { }

        public Book(String ID, String Name, String Author, double Price, String Press, DateTime ReleaseTime)
        {
            this.ID = ID;
            this.Name = Name;
            this.Author = Author;
            this.Price = Price;
            this.Press = Press;
            this.ReleaseTime = ReleaseTime;
        }

    }
}

程序集引入,一个mysql(需要引用MySql.Data.dll),一个sqlserver

using MySql.Data.MySqlClient;
using System.Data.SqlClient;
private readonly string sqlconnectionString = "server=10.19.*.*;Uid=sa;Pwd=SS;Database=TestDB";
private readonly string mysqlconnectionString = "server=localhost;User Id=root;Database=mysql;Port=3306;Password=**";

//查询数据
IEnumerable<Book> book_lists;
using (IDbConnection conn = new MySqlConnection(mysqlconnectionString))
{
    book_lists = conn.Query<Book>("select * from Book").ToList();
}

foreach (Book book_list in book_lists)
{
    MessageBox.Show(book_list.Author);
}

//插入数据
Book book1 = new Book("5", "t1", "wyz", 10.6, "yh", DateTime.Now);

using (IDbConnection conn = new MySqlConnection(mysqlconnectionString))
{
    //两种插入数据的方法
    //var r = conn.Execute(@"insert  Book values (@ID, @Name, @Author, @Price, @Press, @ReleaseTime)",
    //new { ID = "4", Name = "letu", Author = "HL", Price = 11, Press = "dd", ReleaseTime = DateTime.Now });
    conn.Execute("insert into Book values (@ID, @Name, @Author, @Price, @Press, @ReleaseTime)", book1);
}

dapper的日志功能+程序报错修改
https://www.cnblogs.com/moshanghuakai/p/7132583.html

2. NLog 日志管理

NLog是一款拥有丰富的途径选择和管理能力的可用于.net、Silverlight和Windows Phone的免费开源框架.它可以将任何.net语言产生的调试信息转化为上下文信息(包括日期和时间,严重程度,进程,线程,环境信息),根据你喜好的形式发送到一个或者多个目标存储。

1.在Program.cs文件中,获取一个Logger实例,取名为NLogConsoleExample(名字可任意),也可采用 LogManager.GetCurrentClassLogger()来获取一个以当前类名为名称的Logger实例;然后我们在main方法中添加几行简单调用NLog输出不同级别的日志信息的代码,如下图:
这里写图片描述
2.接下来,开始配置NLog配置文件,NLog配置文件支持两种方式:
1)是将配置写到应用程序的配置文件(通常是applicationName.exe.config)或者Web.config文件中;
2)独配置到一个文件,通常是NLog.config 添加的NLog.config文件必须放在项目对应的DEBUG目录或RELEASE目录中,即必须放在和可执行文件相同的路径中
这里采用第二种方法,

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Trace"
      internalLogFile="${basedir}/logs/internal-nlog.txt">
  <targets xsi:type="AsyncWrapper">
    <target name="log_file" xsi:type="File"
                    fileName="${basedir}/logs/${shortdate}.log"/>
  </targets>

  <rules>
    <logger name="*" level="Info" writeTo="log_file" />
    <logger name="*" level="Error" writeTo="log_file" />
    <logger name="*" level="Debug" writeTo="log_file" />
    <logger name="*" level="Fatal" writeTo="log_file" />
    <logger name="*" level="Warn" writeTo="log_file" />
  </rules>
</nlog>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huangle63

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值