.NET Core 数据库连接字符串 属性 尚未初始化

.NET Core Database The Connection String property has not been initialized

我正在尝试构建一个显示数据库中用户记录列表的 .NET 6 Core MVC 应用程序。 但是在尝试打开连接时出现“连接字符串 属性 尚未初始化”错误。 现在我正尝试在查询字符串中提供数据但出现此错误。

**DATA ACCESS LAYER**

using ABL_USER_DebitCard_Info.Models;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace ABL_USER_DebitCard_Info.Context
{
    public class DebitCard_DAL
    {

        string connectionString = "Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = ABL_DebitCard_User_Info_DB";

        
        public IEnumerable<Users> GetUserByCNIC(string? CNIC)
        {
            var debitcardList = new List<Users>();

            using (SqlConnection conn = new SqlConnection())
            {
                SqlCommand cmd = new SqlCommand("ABL_GetUserByCNIC", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CNIC", CNIC);
                

conn.Open(); ---> GETTING ERROR HERE

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var user = new Users();
                    user.Id = Convert.ToInt32(reader["ID"].ToString);
                    user.CNIC = reader["CNIC"].ToString();
                    user.UserName = reader["UserName"].ToString();
                    user.CardNumber = reader["CardNumber"].ToString();
                    user.CardStatus = reader["CardStatus"].ToString();

                    debitcardList.Add(user);
                }
                conn.Close();
            }
            return debitcardList;
        }

    }
}


**Controller**

using ABL_USER_DebitCard_Info.Context;
using ABL_USER_DebitCard_Info.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ABL_USER_DebitCard_Info.Controllers
{
    public class UsersController : Controller
    {

        DebitCard_DAL dbcontext = new DebitCard_DAL();

        [HttpGet]
        public ActionResult Details(string CNIC)
        {
            if(CNIC == null)
            {
                return NotFound();
            }
            else
            {
                List<Users> debitcardList = dbcontext.GetUserByCNIC(CNIC).ToList();
                return View(debitcardList);
                if(debitcardList.Count == 0)
                {
                    return NotFound();
                }
                else
                {
                    return View(debitcardList);
                }
            }
            
        }



    }
}

您在未提供任何连接字符串的情况下创建了 SqlConnection。 将连接字符串传递给 SqlConnection 构造函数 using (SqlConnection conn = new SqlConnection(connectionString ))

你有一个名为“connectionString”的变量很好,但不要忘记在需要时使用它:)

在创建 sql 连接的新实例时,您忘记放置它了。

using (SqlConnection conn = new SqlConnection(connectionStringGoesHere))

connectionstring必须在startup.cs中提及 使用这个或者你也可以使用 dapper 运行 .net core

中的查询

using (SqlConnection conn = new SqlConnection(connectionString)