hisql 新一代无实体ORM使用第一步 hisql安装使用

安装

github hisql 最新源码下载
也可以通过nuget安装
hisql.net 官网(文档编写中)

HiSql 查询语句教程
hisql orm 框架insert数据写入教程
hisql与目前比较流行的ORM框架性能测试对比

注意:HiSql支持framework4.6 .netcore .net5或以上环境

  1. 选择您需要支持的数据库对应的支持包进行安装 本例使用sqlserver进行演示,请安装1.0.1.3以上版本

  2. 连接数据库

  HiSqlClient sqlclient = new HiSqlClient(
      new ConnectionConfig()
               {
                   DbType = DBType.SqlServer,
                   DbServer = "local-HoneBI",
               //ConnectionString = "server=192.168.1.90,8433;uid=sa;pwd=Hone@123;database=HoneBI",
               ConnectionString = "server=(local);uid=sa;pwd=Hone@123;database=HiSql;",//; MultipleActiveResultSets = true;
               Schema = "dbo",
                   IsEncrypt = true,
                   IsAutoClose = false,
                   SqlExecTimeOut = 60000,

                   AppEvents = new AopEvent()
                   {
                       OnDbDecryptEvent = (connstr) =>
                       {
                       //解密连接字段
                       //Console.WriteLine($"数据库连接:{connstr}");

                       return connstr;
                       },
                       OnLogSqlExecuting = (sql, param) =>
                       {
                       //sql执行前 日志记录 (异步)

                       //Console.WriteLine($"sql执行前记录{sql} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
                   },
                       OnLogSqlExecuted = (sql, param) =>
                       {
                       //sql执行后 日志记录 (异步)
                       //Console.WriteLine($"sql执行后记录{sql} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
                   },
                       OnSqlError = (sqlEx) =>
                       {
                       //sql执行错误后 日志记录 (异步)
                          Console.WriteLine(sqlEx.Message.ToString());
                       },
                       OnTimeOut = (int timer) =>
                       {
                       //Console.WriteLine($"执行SQL语句超过[{timer.ToString()}]毫秒...");
                   }
                   }
               }
               );

            

  1. 第一次使用时需要执行以下方法进行HiSql安装(只需要执行一次)
sqlclient.CodeFirst.InstallHisql();

4 .检查是否安装成功
数据库中出现以下四张表则安装成功

下面就可以进行表操作了
建一张测试表【H_Test】 测试一下 sql代码如下

USE [HiSql]
GO

/****** Object:  Table [dbo].[H_Test]    Script Date: 2021/10/26 9:37:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[H_Test](
	[Hid] [int] NOT NULL,
	[UserName] [nchar](50) NULL,
	[UserAge] [int] NULL,
	[ReName] [nvarchar](50) NULL,
 CONSTRAINT [PK_H_Test] PRIMARY KEY CLUSTERED 
(
	[Hid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[H_Test] ADD  CONSTRAINT [DF_H_Test_UserAge]  DEFAULT ((0)) FOR [UserAge]
GO


或可以通过实体类来创建表 ,如下实体

[HiTable(IsEdit = true, TabName = "H_Test", TabDescript = "测试表")]
    public class H_Test: StandField
    {
        [HiColumn(FieldDesc = "编号",  IsPrimary = true, IsBllKey = true,   DBDefault = HiTypeDBDefault.EMPTY, SortNum = 1)]
        public int Hid { get; set; }

        [HiColumn(FieldDesc = "用户名", FieldLen = 50,   DBDefault = HiTypeDBDefault.EMPTY, SortNum = 2 )]
        public string UserName { get; set; }

        [HiColumn(FieldDesc = "年龄",  DBDefault = HiTypeDBDefault.EMPTY, SortNum = 3)]
        public int UserAge { get; set; }

        [HiColumn(FieldDesc = "真实姓名", FieldLen = 50, DBDefault = HiTypeDBDefault.EMPTY, SortNum = 4)]
        public string ReName { get; set; }
    }

再执行以下代码

Tuple<HiTable, List<HiColumn>> tabomdel = sqlclient.Context.DMInitalize.BuildTabStru(typeof(DemoTable.H_Test));
            int v=(int)sqlclient.Context.DBO.ExecCommand(sqlclient.Context.DMTab.BuildTabCreateSql(tabomdel.Item1, tabomdel.Item2, true));

  1. 通过HiSql ORM SDK 向表[H_Test] 插入数据
sqlclient .Insert("H_Test", new{Hid = 1, UserName = "tansar",UserAge = 100, ReName = "Tom"}).ExecCommand();
  1. 查询刚刚插入的数据
DataTable dt_result = sqlclient.Query("H_Test").Field("*").ToTable();

posted @ 2021-10-26 10:22  tansar  阅读(528)  评论(0编辑  收藏  举报