1

C# 操作国产数据库【 人大金仓 】之四大模式

 6 months ago
source link: https://www.cnblogs.com/sunkaixuan/p/18055087
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

人大金仓优点

人大金仓是国产最主流数据库之一

具有和PgSql一样强悍的性能,同时人大金仓支持了四种数据库模式 :

Oracle、PgSql、MySql和SqlServer ,假如你们系统有多种数据库开发

有国产化要求,那么你们切换到人大金仓就会很方便了

Nuget安装 C# ORM

搜索人大金仓安装前2个

746906-20240306131835710-1581282021.png

SqSugar和人大金仓官方有深层次的合作,SqlSugar在人大金仓的支持上非常的全面

不是简简单的去实现CRUD,而是把人大金仓的每个特性都支持的很好 

Oracle模式的存储过程、Schema

 R3、 R6 Oracle模式、R6 PgSql模式 、R6 MySql模式和R6 SqlServer模式

建库 、建表和SQL函数等都完美支持

数据库版本配置

每个版本都有些注意点,有些需要提定一下模式,有些需要升级一下nuget

R3 老版本

直接安装 和使用

R6:Oracle模式(推荐默认 )

 支持存储过程 和 PostgreSQL语法 (推荐)

R6:MySql模式 ( 需要独立最新 )

SqlSugarCore.Kdbndp 到最新版本

R6:PostgreSQL模式(需配置)

配置一下pg模式如下

//SqlSugarCore 5.1.4.143-preview08 支持
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.Kdbndp,
ConnectionString ="Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1",
IsAutoCloseConnection = true,
MoreSettings=new ConnMoreSettings()
{
//SqlSugarCore 5.1.4.143
DataBaseModel= DbType.PostgreSQL//配置PG模式主要是兼容系统表差异
}
})

R6:SqlServer模式(需配置)

如何要使用CodeFirst需要配置

SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.Kdbndp,
ConnectionString ="Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1",
IsAutoCloseConnection = true,
MoreSettings=new ConnMoreSettings()
{
//SqlSugarCore 5.1.4.144-preview16+ 支持
DataBaseModel= DbType.SqlServer//配置SqlServer模式主要是兼容CodeFirst报错
}
})

已知问题:

1、Date类型不支持,只支持DateTime类型 金仓官方在开发了

2、it.时间.Date==时间.Date 要改成 it.时间.ToString("yyyy-MM-dd")==时间.ToString("yyyy-MM-dd")

3、表模式

2种模式用法小有区别,推荐规范表

1. 规范表: 自动转大写  

2. 驼峰表: 不自动转大写

规范(自动转大写) 

表名 STUDENT  字段 ID  NAME   ,直接用就行了SqlSugar不需要设置

SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.Kdbndp,
ConnectionString ="Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1",
IsAutoCloseConnection = true
});
//自动生成下划线看PostgreSQL文档用法差不多

不规范(不转换大写)

 需要配置禁用转大写

SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.Kdbndp,
ConnectionString = "Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1",
IsAutoCloseConnection = true,
MoreSettings=new ConnMoreSettings() {
IsAutoToUpper=false //禁用自动转成大写表 5.1.3.41-preview08
}
});
//注意:请升级到 5.1.3.41-preview08 以上版本  

5、操作人大金仓数据库

using SqlSugar;
SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "datasource=demo.db",
DbType = DbType.Kdbndp,//MySql和SqlServer模式看上面需要配置DatabaseModel
IsAutoCloseConnection = true
});
//建库
Db.DbMaintenance.CreateDatabase();//达梦和Oracle不支持建库
//建表(看文档迁移)
Db.CodeFirst.InitTables<Student>(); //所有库都支持    
//查询表的所有
var list = Db.Queryable<Student>().ToList();
//插入
Db.Insertable(new Student() { SchoolId = 1, Name = "jack" }).ExecuteCommand();
//更新
Db.Updateable(new Student() { Id = 1, SchoolId = 2, Name = "jack2" }).ExecuteCommand();
//删除
Db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
//实体与数据库结构一样
public class Student
{
//数据是自增需要加上IsIdentity
//数据库是主键需要加上IsPrimaryKey
//注意:要完全和数据库一致2个属性
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public int? SchoolId { get; set; }
public string? Name { get; set; }
}

6、架构支持 schema(非Public)

连接字符串上加上 searchpath=架构名 ,可以支持多架

7、字符串空判段问题

XI(LJ0SUBJ_R%(BO1(TTGEH.png

8、.NET Framework用户dll

需要引用的dll ,官方定制比外面找的dll更加强大  ( .NET Core用户直接安装SqlSugarCore就可以了)

icon_rar.gifKdbndp_dll.rar   framework用户用R6Oracle或者R3 只有.NET Core支持了四种模式

9、常见问题

 9.1 string ==""无效

oracle模式下没空只有null, 多库用户可以配置一下off参数

ora_input_emptystr_isnull = off  这个参数放到,kingbase.conf的结尾就可以。

源码和安装


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK