简体   繁体   English

尝试读取 C# .net 6.0 控制台应用程序中的 ConnectionString 值时,得到 Object 参考 Z37A6259CC0C8DAEFF02 错误

[英]While trying to read ConnectionString value in C# .net 6.0 console app getting Object reference null error

I have a console application(.Net 6.0) with app.config file with following data.我有一个带有 app.config 文件的控制台应用程序(.Net 6.0),其中包含以下数据。

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
<connectionStrings>
    <add name ="ConnectionKey" connectionString="my connection value" providerName="System.Data.SqlClient"/>
</connectionStrings>

I am trying to read the connection string the following way but I get the error "Object reference not set to an instance of an object."我试图通过以下方式读取连接字符串,但出现错误“对象引用未设置为 object 的实例”。

 cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionKey"].ConnectionString);

Is there something more I have to add?我还有什么要补充的吗? Or is there any other approach to read the connectionString value?还是有其他方法可以读取 connectionString 值?

Update:更新:

I have changed the app.config to appsettings.json with following code我已使用以下代码将 app.config 更改为 appsettings.json

{
"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true",
    "MyConn": "my connection string"
}
  }

You can get the connection string like below:您可以获得如下连接字符串:

public class HomeController : Controller
{
    public readonly IConfiguration _configuration;
    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IActionResult Index()
    {
        string conn = _configuration.GetConnectionString("MyConnection");
        var  cnx = new SqlConnection(conn);
        //do your stuff...
        return View();
    }
} 

appsettings.json: appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
      "MyConnection": "your connection string"        //focus here...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 package 控制台管理器更新数据库时出现此错误值不能是 null。 (参数“连接字符串”) - While updating database from package console manager getting this error Value cannot be null. (Parameter 'connectionString') 无法在 Linux 上运行 C# 控制台应用程序 .net 6.0) - Fail to run C# console app (net 6.0) on Linux 当我在 asp.net core 3.1 中工作时收到此错误值不能为空。 (参数&#39;connectionString&#39;) - While I'm working ins asp.net core 3.1 getting this error Value cannot be null. (Parameter 'connectionString') 在C#控制台应用程序中添加参考错误 - Add reference Error in c# console app 获取数据是 Null 错误,同时尝试在 SQL 位数据类型中使用 Linq 查询获取包含错误值的记录 - Getting Data is Null error while trying to fetch record containing false value in SQL bit datatype using Linq query in C# 来自控制台应用程序的 Active Directory 更新引发 Object 参考错误,以前的值为 null - Active Directory update from Console App throwing Object Reference Error, where the value was null previously .NET 6.0 C#“新控制台模板”- 如何阅读 CLI arguments? - .NET 6.0 C# "new console template" - how to read CLI arguments? 尝试在.net框架中使用C#代码为数据库增加价值时; 出现错误:名称“名称”在当前上下文中不存在 - While trying to add value into Database, using C# code in .net framework; getting Error: The name 'name' does not exist in the current context Oracle DB 未从 C# 控制台应用程序在 Visual Studio Code 中构建在 net6.0 上 - Oracle DB not connecting from C# Console App built on net6.0 in Visual Studio Code C#.Net 6.0 应用程序的 Console.WriteLine 不是 output 到 Linux 上的 /bin/bash - C# .Net 6.0 App's Console.WriteLine not output to /bin/bash on Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM