发布:2023/12/7 15:52:31作者:大数据 来源:大数据 浏览次数:668
nuget安装依赖:
Microsoft.Extensions.Configuration.Json
根据自身环境选择对应版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Program.cs using Microsoft.Extensions.Configuration; using System; using System.IO; namespace ConsoleApp1 { class Program { static void Main(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() //.SetBasePath(Directory.GetCurrentDirectory())//此句可省略,默认读取当前目录 .AddJsonFile(path: "appsettings.json", optional: true, reloadOnChange: true) .Build(); string defaultConnectionString = configuration.GetConnectionString("DefaultConnectionString"); Console.WriteLine(defaultConnectionString); Console.ReadKey(); } } } |
注意:configuration.GetConnectionString("DefaultConnectionString")这里只写ConnectionStrings节点下的名称即可。
另外需要设置appsettings.json属性为始终复制和内容,要不找不到文件
1 2 3 |
"ConnectionStrings": { "DefaultConnectionString": "Server=127.0.0.1;port=3306;User Id=root;Password=root;Database=test_db" }, |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4