数据库
首页 > 数据库> > hisql 新一代无实体ORM使用第一步 hisql安装使用

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

作者:互联网

安装

注意:HiSql仅支持.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();

标签:Console,int,使用,sqlclient,ORM,sql,Test,hisql,true
来源: https://www.cnblogs.com/tansar/p/15464369.html