主題介紹

本文主要介紹Net Core框架下如何連接瀚高數(shù)據(jù)庫(kù)。

驅(qū)動(dòng)介紹

鏈接:https://pan.baidu.com/s/1xuz6uJz0utRgKWecXhpOiA?pwd=o0tj

瀚高數(shù)據(jù)庫(kù)安裝完畢后,自帶數(shù)據(jù)庫(kù)驅(qū)動(dòng),其位置如下:

image

其中 DotNet 文件夾 即為 Net 程序使用的驅(qū)動(dòng)包,進(jìn)入目錄

image

解壓后,可以看到

image

其中列出了 net5.0、netcoreapp3.1、netstandard2.0、netstanddard2.1Nhgb.EntityFrameworkcore.HGDB.dll 等幾個(gè)文件,根據(jù)開(kāi)發(fā)環(huán)境和框架,從中選擇適用的驅(qū)動(dòng)。

Net Core

.NET Core平臺(tái)是免費(fèi)的、開(kāi)源的。

.NET Core的主要特性包括開(kāi)源、跨平臺(tái)、現(xiàn)代、靈活、輕量級(jí)、快速、友好、可共享,以及為未來(lái)的軟件開(kāi)發(fā)而構(gòu)建的。

.NET Core是一個(gè)通用的軟件開(kāi)發(fā)框架。它允許開(kāi)發(fā)人員構(gòu)建各種軟件,包括Web,桌面,移動(dòng),云,游戲,物聯(lián)網(wǎng)等。

.NET Core支持并運(yùn)行在Windows、MacOS和Linux操作系統(tǒng)上。.NET Core跨體系結(jié)構(gòu)(包括x64、x86和ARM)是一致的??梢詫?dǎo)入相同的程序集和庫(kù),并在多個(gè)平臺(tái)上使用。這些程序集和庫(kù)都可以使用如下的.NET語(yǔ)言進(jìn)行構(gòu)建,如:C#、VB.NET或F#。

.NET Core通過(guò).NET Standard與.NET Framework,Xamarin和Mono兼容。

ASP.NET Core是.NET Core生態(tài)系統(tǒng)的核心組件。ASP.NET Core是一個(gè)用于構(gòu)建網(wǎng)頁(yè)的框架。ASP.NET Core基于MVC架構(gòu),并提供用于構(gòu)建Web的通用庫(kù)。

.NET Core的版本截止為3.1。

.NET 5 和 .Net Core 官方支持策略 (microsoft.com)

image

根據(jù) 微軟官方文檔的介紹.NET Standard - .NET | Microsoft Learn的介紹

  • netstandard2.0

image

  • netstandard2.1

image

綜上所述:使用Net Core框架的項(xiàng)目,首選需要引用 netcoreapp3.1 文件中的驅(qū)動(dòng)。

開(kāi)發(fā)環(huán)境搭建

軟件 版本
HGDB 安全版V4、企業(yè)版v6及以上版本
visual studio 2022

添加引用

image

目標(biāo)框架

image

示例代碼

Student.cs

Text
public class Student
{
public int Id { get; set; }

public string Name { get; set; }

public string Sex { get; set; }

public int Age { get; set; }

public int Grade { get; set; }

public string Address { get; set; }

}

StudentViewModel.cs

Text
public class StudentViewModel
{
public List<Student> Students { get; set; }
}

StudentController.cs

Text
public class StudentController : Controller
{
public IActionResult Index()
{
DataTable dt = new DataTable();
string sql = "select id as Id,name as Name,sex as Sex,age as Age,grade as Grade,address as Address from student order by id";

var connString = "Server = 192.168.2.5; Port = 5866; User Id = sysdba; Password = Hero@123; Database = highgo;";
var conn = new NhgdbConnection(connString);
conn.Open();

NhgdbCommand sc = new NhgdbCommand(sql);
sc.Connection = conn;
sc.CommandTimeout = 0;

NhgdbDataAdapter adapter = new NhgdbDataAdapter(sc);
adapter.Fill(dt);

// 把DataTable轉(zhuǎn)換為IList<UserInfo>
IList<Student> students = ModelConvertHelper<Student>.ConvertToModel(dt);
return View(new StudentViewModel { Students =students.ToList() });
}
}

Index.cshtml

Text
<div class="panel panel-default todo-panel">
<div class="panel-heading">@ViewData["Title"]</div>
<table class="table table-hover">
<thead>
<tr>
<td> <input type="checkbox" class="done-checkbox"></td>
<td>序號(hào)</td>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
<td>年級(jí)</td>
<td>地址</td>
</tr>
</thead>
@foreach (var item in Model.Students)
{
<tr>
<td>
<input type="checkbox" class="done-checkbox">
</td>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.Sex</td>
<td>@item.Age</td>
<td>@item.Grade</td>
<td>@item.Address</td>
</tr>
}
</table>
</div>

結(jié)果演示

image

在Net5.0+場(chǎng)景中的表現(xiàn)

image

經(jīng)測(cè)試,在6.0、7.0、8.0的場(chǎng)景下,本示例均可正常運(yùn)行。

需要注意的是,當(dāng)目標(biāo)框架不存在時(shí),vs2022會(huì)提示需要安裝,點(diǎn)擊確定后,會(huì)自動(dòng)下載。完了安裝即可。

引用其他版本驅(qū)動(dòng)的表現(xiàn)

引用 netstanddard2.0

image

運(yùn)行應(yīng)用報(bào)錯(cuò)

image

引入Microsoft.Bcl.AsyncInterfaces 1.0.0 版本后,可正常運(yùn)行應(yīng)用

引用 netstanddard2.1

不需引入其他依賴(lài),即可正常運(yùn)行。

image

引用 net5.0

版本沖突,暫時(shí)不能添加高版本的依賴(lài)dll

image

目標(biāo)框架改為.NET 5.0后可正常使用。

引用 net6.0

版本沖突

image

目標(biāo)框架改為.NET 6.0后可正常使用。

引用 net7.0

版本沖突

image

目標(biāo)框架改為.NET 7.0后可正常使用。

引用 net8.0

版本沖突

image

目標(biāo)框架改為.NET 8.0后可正常使用。