1. 程式人生 > >[開源] .Net 使用 ORM 訪問 華為GaussDB資料庫

[開源] .Net 使用 ORM 訪問 華為GaussDB資料庫

## 前言 華為GaussDB是一個企業級AI-Native分散式資料庫。GaussDB採用MPP(Massive Parallel Processing)架構,支援行儲存與列儲存,提供PB(Petabyte,2的50次方位元組)級別資料量的處理能力。可以為超大規模資料管理提供高性價比的通用計算平臺,也可用於支撐各類資料倉庫系統、BI(Business Intelligence)系統和決策支援系統,為上層應用的決策分析提供服務。 隨著華為、中興事務,國產資料庫市場相信是未來是趨勢走向,縱觀 .net core 整個圈子對國產華為GaussDB資料庫的支援幾乎為 0,今天我們使用 FreeSql ORM 來體驗國產華為GaussDB資料庫(雖然是拿pgsql原始碼修改的)。 整體來講,華為GaussDB 和 pgsql 語法相容,但是目前只能通 ODBC 訪問(沒有 ado.net 驅動,或許 npgsql 也可以訪問,如果有小夥伴知道請 at 我們),FreeSql 提供 ODBC 多種資料庫訪問實現。 > 由於本人沒有測試環境,但是群友使用文章內的方法已經可以成功訪問。本文的圖片引用是其他資料庫的截圖,大致情況一樣。 ## 1、安裝環境 資料庫伺服器:華為GaussDB > 下載地址:https://e.huawei.com/cn/products/cloud-computing-dc/gaussdb .NET版本:.net core 3.1 > 下載地址:https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install 開發機器 :windows 10 ODBC驅動:請按照華為GaussDB文件安裝 ## 2、建立專案 我們以 console 型別專案試驗 插入、刪除、更新、查詢 等功能,建立控制檯專案,使用命令: > dotnet new console ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528180608428-950673665.png) > dotnet add package FreeSql.Provider.Odbc > dotnet add package FreeSql.Repository ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528181146464-1147836030.png) ## 3、建立實體模型 ``` using System; using FreeSql.DataAnnotations; public class User { [Column(IsIdentity = true)] public long Id { get; set; } public string UserName { get; set; } public string PassWord { get; set; } public DateTime CreateTime { get; set; } } ``` ## 4、初始化 ORM ```csharp static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.OdbcPostgreSQL, "odbc 連線字串") .UseMonitorCommand(cmd => Trace.WriteLine($"執行緒:{cmd.CommandText}\r\n")) .UseAutoSyncStructure(true) //自動建立、遷移實體表結構 .UseNameConvert(NameConvertType.ToUpper) .Build(); ``` ## 5、插入資料 ```csharp var repo = fsql.GetRepository(); var user = new User { UserName = "gaussdb1", PassWord = "123" }; repo.Insert(user); ``` ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528181506243-2124993119.png) ```csharp var users = new [] { new User { UserName = "gaussdb2", PassWord = "1234" }, new User { UserName = "gaussdb3", PassWord = "12345" }, new User { UserName = "gaussdb4", PassWord = "123456" } }; repo.Insert(users); //批量插入 ``` ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528181748212-1867325535.png) ## 6、更新資料 ```csharp user.PassWord = "123123"; repo.Update(user); ``` ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528181840576-1860907762.png) ## 7、查詢資料 ```csharp var one = fsql.Select(1).First(); //查詢一條資料 var list = fsql.Select().Where(a => a.UserName.StartsWith("gaussdb")).ToList(); ``` ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528181945233-1054833858.png) ![](https://img2020.cnblogs.com/blog/1694977/202005/1694977-20200528182033212-2097599619.png) ## 8、刪除資料 ```csharp fsql.Delete(1).ExecuteAffrows(); fsql.Delete().Where(a => a.UserName.StartsWith("gaussdb")).ExecuteAffrows(); ``` ## 結語 這篇文章簡單介紹了在 .net core 3.1 環境中使用 FreeSql 對國產華為GaussDB資料庫的訪問,目前 FreeSql 還支援 .net framework 4.0 和 xamarin 平臺上使用。 國產資料庫未來是發展趨勢,擁有自主權不受他人限制,我在好幾個群裡看到有人說公司正準備全面使用國產系統+國產資料庫。 除了 增刪查改,FreeSql 還支援很多功能,就不一一演示,一篇文章介紹不完。 FreeSql 是 .NETCore/.NetFramework/Xamarin 平臺下的 ORM 開源專案,支援 SqlServer/MySql/PostgreSQL/Oracle/Sqlite/達夢,還有華為GaussDB資料庫,未來還會接入更多的國產資料庫支援。 原始碼地址:[https://github.com/2881099/FreeSql](https://github.com/2881099/FreeSql) 謝謝