C# 操作国产数据库【 人大金仓 】之四大模式
<h2>人大金仓优点</h2><p>人大金仓是国产最主流数据库之一</p>
<p>具有和PgSql一样强悍的性能,同时人大金仓支持了四种数据库模式 :</p>
<p>Oracle、PgSql、MySql和SqlServer ,假如你们系统有多种数据库开发</p>
<p>有国产化要求,那么你们切换到人大金仓就会很方便了</p>
<p> </p>
<h2 id="blogTitle0">Nuget安装 C# ORM</h2>
<p>搜索人大金仓安装前2个</p>
<p><img src="https://img2024.cnblogs.com/blog/746906/202403/746906-20240306131835710-1581282021.png" alt="" loading="lazy"></p>
<p> </p>
<p>SqSugar和人大金仓官方有深层次的合作,SqlSugar在人大金仓的支持上非常的全面</p>
<p>不是简简单的去实现CRUD,而是把人大金仓的每个特性都支持的很好 </p>
<p>Oracle模式的存储过程、Schema</p>
<p> R3、 R6 Oracle模式、R6 PgSql模式 、R6 MySql模式和R6 SqlServer模式</p>
<p>建库 、建表和SQL函数等都完美支持</p>
<p> </p>
<h2 id="blogTitle1">数据库版本配置</h2>
<p>每个版本都有些注意点,有些需要提定一下模式,有些需要升级一下nuget</p>
<h3 id="blogTitle2">R3 老版本</h3>
<p>直接安装 和使用</p>
<h3 id="blogTitle3">R6:Oracle模式(推荐默认 )</h3>
<p> 支持存储过程 和 PostgreSQL语法 (推荐)</p>
<h3 id="blogTitle4">R6:MySql模式 ( 需要独立最新 )</h3>
<p>SqlSugarCore.Kdbndp 到最新版本</p>
<h3 id="blogTitle5">R6:PostgreSQL模式(需配置)</h3>
<p>配置一下pg模式如下</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">//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模式主要是兼容系统表差异
}
})</pre>
</div>
<h3 id="blogTitle6">R6:SqlServer模式(需配置)</h3>
<p>如何要使用CodeFirst需要配置</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">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报错
}
})</pre>
</div>
<p>已知问题:</p>
<p>1、Date类型不支持,只支持DateTime类型 金仓官方在开发了</p>
<p>2、it.时间.Date==时间.Date 要改成 it.时间.ToString("yyyy-MM-dd")==时间.ToString("yyyy-MM-dd")</p>
<p> </p>
<h2 id="blogTitle7">3、表模式</h2>
<p>2种模式用法小有区别,推荐规范表</p>
<p>1. 规范表: 自动转大写 </p>
<p>2. 驼峰表: 不自动转大写</p>
<h3 id="blogTitle8">规范(自动转大写) </h3>
<p>表名 STUDENT 字段 ID NAME ,直接用就行了SqlSugar不需要设置</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> 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文档用法差不多</pre>
</div>
<h3 id="blogTitle9">不规范(不转换大写)</h3>
<p> 需要配置禁用转大写</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> 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 以上版本 </pre>
</div>
<p> </p>
<h2 id="blogTitle11">5、操作人大金仓数据库</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">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个属性
public int Id { get; set; }
public int? SchoolId { get; set; }
public string? Name { get; set; }
}
</pre>
</div>
<p> </p>
<h2 id="blogTitle12">6、架构支持 schema(非Public)</h2>
<p>连接字符串上加上 searchpath=架构名 ,可以支持多架</p>
<p> </p>
<h2 id="blogTitle13">7、字符串空判段问题</h2>
<p><img src="https://www.donet5.com/_theme/ueditor/utf8-net/net/upload/image/20211108/6377196375676288916631790.png" alt="XI(LJ0SUBJ_R%(BO1(TTGEH.png" title="XI(LJ0SUBJ_R%(BO1(TTGEH.png"></p>
<p> </p>
<h2 id="blogTitle14">8、.NET Framework用户dll</h2>
<p>需要引用的dll ,官方定制比外面找的dll更加强大 ( .NET Core用户直接安装SqlSugarCore就可以了)</p>
<p><img src="https://www.donet5.com/_theme/ueditor/utf8-net/dialogs/attachment/fileTypeImages/icon_rar.gif">Kdbndp_dll.rar framework用户用R6Oracle或者R3 只有.NET Core支持了四种模式</p>
<p> </p>
<h2 id="blogTitle15">9、常见问题</h2>
<h3 id="blogTitle16"> 9.1 string ==""无效</h3>
<p>oracle模式下没空只有null, 多库用户可以配置一下off参数</p>
<p>ora_input_emptystr_isnull = off 这个参数放到,kingbase.conf的结尾就可以。</p>
<p> </p>
<h2 id="blogTitle1">源码和安装</h2>
<table>
<tbody>
<tr class="firstRow">
<td valign="top" width="93.33333333333331">Github源码</td>
<td valign="top" width="956">https://github.com/donet5/SqlSugar </td>
</tr>
<tr>
<td valign="top" width="93.33333333333331">Gitee源码</td>
<td valign="top" width="956">https://gitee.com/dotnetchina/SqlSugar </td>
</tr>
<tr>
<td rowspan="1" colspan="1" valign="top">开源生态</td>
<td rowspan="1" colspan="1" valign="top">https://www.donet5.com/Home/Doc?typeId=1215</td>
</tr>
<tr>
<td valign="top" width="93.33333333333331">Nuget</td>
<td valign="top" width="956">https://www.donet5.com/Home/Doc?typeId=1226</td>
</tr>
<tr>
<td rowspan="1" colspan="1" valign="top">AOT</td>
<td rowspan="1" colspan="1" valign="top">https://www.donet5.com/Home/Doc?typeI</td>
</tr>
</tbody>
</table><br><br>
来源:https://www.cnblogs.com/sunkaixuan/p/18055087
頁:
[1]