栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

(2) 增加一些基础类,引用数据访问类库

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(2) 增加一些基础类,引用数据访问类库

一些类还是必须的。比如读取配置文件。

在项目下我建一个文件夹code

codeutilsWebHelper.cs

using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Security.Claims;
using System.Text;



namespace core_admin.utils
{
    public class WebHelper
    {
        static IConfigurationRoot? Configuration{get;set;}
        
        public static void config()
        {
            var builder = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json");           

            Configuration = builder.Build();
     
        }


        public static string get_item(string key)
        {
            if(Configuration==null){
                config();
            }
            string val = Configuration.GetSection("AppSettings").GetValue(key);
            if (val == null)
                val = "";
            return val;
        }

        public static int page_size = 10;

    }
}

用来读取配置文件。

codeutilsGlobal_Values.cs

using System;

namespace core_admin.utils
{
    public class Global_Values
    {
        public static string connStr = "";

        public static string table_prefix = "";

    }
}

用来存储数据链接字符串

然后我选择了sqlsugar 来访问数据。

在cmd下运行 

dotnet add package sqlSugarCore

然后,可以看到MyWebApp.csproj 这个项目文件里增加了对sqlSugarCore的包引用

现增加一个Db工厂类

codedbDbFactory.cs

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections;
using core_admin.utils;

namespace core_admin.db
{
    public class DbFactory
    {

        public static DbType db_type = DbType.MySql;   

        /// 
        /// Create SqlSugarClient
        /// 
        /// 
        public static SqlSugarClient GetInstance()
        {
            DbType t = db_type;            
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = Global_Values.connStr,
                DbType = t,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });            
            db.MappingTables.AddRange(SugarConfigs.MpList);
            //Print sql
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "rn" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            return db;
        }
    }

    /// 
    /// 全局配置类
    /// 
    public class SugarConfigs
    {
        //key类名 value表名
        public static List MpList
        {
            get
            {
                Hashtable table_hash = new Hashtable();
                string[] tables = new string[] { };                
                List list = new List();
                foreach(string a_table in tables)
                {
                    if (!table_hash.ContainsKey(a_table))
                    {
                        list.Add(new MappingTable()
                        {
                            EntityName = a_table,
                            DbTableName = Global_Values.table_prefix + a_table
                        });
                        table_hash.Add(a_table, a_table);
                    }                  
                }
                return list;
            }
        }
            
    }
}

然后在Program.cs开头增加两行,

using core_admin.utils;

Global_Values.connStr = WebHelper.get_item("connStr");

相应的,在appsettings.json里增加几项,

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AppSettings": {  
    "db_type": "mysql",
    "connStr": "Server=localhost;Port=3306;Database=db_blog;User=root;Password=12345678;Charset=utf8mb4;SslMode = none;allowPublicKeyRetrieval=true"
  }
}

为了观察一下Global_Values.connStr 是否已经正常赋值,在Views/Home/Index.cshtml里增加一行

@{
    ViewData["Title"] = "Home Page";
}


    

Welcome

Learn about building Web apps with ASP.NET Core.

@core_admin.utils.Global_Values.connStr

再运行web , dotnet run

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号