.net core下配置訪問(wèn)數(shù)據(jù)庫(kù)操作
配置讀取
.net core下讀取配置還是有點(diǎn)麻煩的,本身沒(méi)有System.Configuration.dll,所以在進(jìn)行配置前需要自行引用Microsoft.Extensions.Configuration,截圖如下:
這樣的話我們就可以配置讀取的相關(guān)編碼了,比如我們數(shù)據(jù)庫(kù)的鏈接字符串,在appsettings.json添加對(duì)應(yīng)的數(shù)據(jù)庫(kù)配置:
"ConnectionStrings": { "TestDb": "server=localhost;port=3306;database=mytest;user=test;password=123456;charset=utf8;" }
讀取配置相關(guān)代碼如下:
public class AppSetting { private static readonly object objLock = new object(); private static AppSetting instance = null; private IConfigurationRoot Config { get; } private AppSetting() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); Config = builder.Build(); } public static AppSetting GetInstance() { if (instance == null) { lock (objLock) { if (instance == null) { instance = new AppSetting(); } } } return instance; } public static string GetConfig(string name) { return GetInstance().Config.GetSection(name).Value; } }
這樣就可以直接讀取對(duì)應(yīng)的配置信息啦:
string CONNECTION_STRING = AppSetting.GetConfig("ConnectionStrings:TestDb");
數(shù)據(jù)庫(kù)操作
數(shù)據(jù)庫(kù)相關(guān)操作還是建議使用Dapper以及Dapper.Contrib,比較輕量,也比較方便。
Dapper相信大家還是比較熟悉的,這里簡(jiǎn)單說(shuō)下Dapper.Contrib,基于Dapper的擴(kuò)展方法,封裝了如下方法:
- T Get<T>(id);
- IEnumerable<T> GetAll<T>();
- int Insert<T>(T obj);
- int Insert<T>(Enumerable<T> list);
- bool Update<T>(T obj);
- bool Update<T>(Enumerable<T> list);
- bool Delete<T>(T obj);
- bool Delete<T>(Enumerable<T> list);
- bool DeleteAll<T>();
這樣對(duì)應(yīng)你應(yīng)用的簡(jiǎn)單的CRUD方法可以很輕松的搞定的。比如下面幾個(gè)例子:
//根據(jù)主鍵Id查詢 using (var conn = DatabaseManager.GetConnection(DatabaseManager.DBName)) { await conn.OpenAsync(); return await conn.GetAsync<UserModel>(id); }
新增數(shù)據(jù):
//新增 using (var conn = DatabaseManager.GetConnection(DatabaseManager.DBName)) { await conn.OpenAsync(); await conn.InsertAsync(entity); }
修改數(shù)據(jù):
//修改 using (var conn = DatabaseManager.GetConnection(DatabaseManager.DBName)) { await conn.OpenAsync(); await conn.UpdateAsync(entity); }
需要注意的是,需要給對(duì)應(yīng)的實(shí)體加上特性:
[Table("User")] public class UserModel { [Key] public int Id { get; set; } public string UserName { get; set; } public string Remark { get; set; } }
- [Table("Tablename")] 標(biāo)識(shí)對(duì)應(yīng)的表名
- [Key] 對(duì)應(yīng)的主鍵
- [ExplicitKey] 如果主鍵不是自增長(zhǎng)的,用此標(biāo)識(shí)
- [Write(true/false)] 該字段是否可被寫入
- Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫(kù)的步驟
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫(kù)
- 淺談如何使用vb.net從數(shù)據(jù)庫(kù)中提取數(shù)據(jù)
- asp.net實(shí)現(xiàn)存儲(chǔ)和讀取數(shù)據(jù)庫(kù)圖片
- .NET Core Dapper操作mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例
- ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫(kù)的方法教程
- ASP.NET WebAPI連接數(shù)據(jù)庫(kù)的方法
- .net core利用orm如何操作mysql數(shù)據(jù)庫(kù)詳解
- .net數(shù)據(jù)庫(kù)操作框架SqlSugar的簡(jiǎn)單入門
相關(guān)文章
asp.net DropDownList實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果
這篇文章主要介紹了asp.net DroDownList實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果的相關(guān)資料,需要的朋友可以參考下2016-02-02Ajax Throws Sys.WebForms.PageRequestManagerErrorException wi
注冊(cè)腳本: ScriptManager.RegisterStartupScript(this, typeof(string), "Message", "alert('error message');", true); 即可解決,困惑了我一下午。2008-11-11asp.net?core?+?jenkins?實(shí)現(xiàn)自動(dòng)化發(fā)布功能
這篇文章主要介紹了asp.net?core?+?jenkins?實(shí)現(xiàn)自動(dòng)化發(fā)布功能,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04IIS上部署Asp.net core Webapi的實(shí)現(xiàn)步驟
ASP.NET Core Web API是構(gòu)建RESTful應(yīng)用程序的理想平臺(tái),本文主要介紹了IIS上部署Asp.net core Webapi的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07Community Server專題一:概述Community Server
Community Server專題一:概述Community Server...2007-03-03asp.net下生成99個(gè)不同的隨機(jī)數(shù)
asp.net下生成99個(gè)不同的隨機(jī)數(shù)...2007-04-04asp.net fileupload控件上傳文件與多文件上傳
這篇文章主要介紹了asp.net fileupload控件上傳文件的方法,fileupload控件多文件上傳,以及fileupload上傳時(shí)實(shí)現(xiàn)文件驗(yàn)證的方法,需要的朋友可以參考下2014-11-11