ConnectionString 属性 尚未使用 ADO 和 NET 5.0 初始化
The ConnectionString property has not been initialized using ADO and NET 5.0
我正在使用 VS 2019,SQL Server 2016,我尝试使用 ADO.NET 更新注册表,但出现错误:
ConnectionString 属性 尚未初始化
控制器:
如果我用这个:
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DevConnection"].ConnectionString);
我收到错误:
Object reference not set to an instance of an object.'
如果我使用:
con = new SqlConnection(appSettings.Value.DbConnection);
我得到:
The ConnectionString property has not been initialized.
其余代码:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(user.UserId));
cmd.Parameters.AddWithValue("@DateOfBirth", user.DateOfBirth);
cmd.Parameters.AddWithValue("@UserNames", user.UserNames);
cmd.Parameters.AddWithValue("@UserLastNames", user.UserLastNames);
cmd.Parameters.AddWithValue("@UserPlatformName", user.UserPlatformName);
cmd.Parameters.AddWithValue("@UserPassword", user.UserPassword);
cmd.Parameters.AddWithValue("@UserGender", user.UserGender);
cmd.Parameters.AddWithValue("@UserDocumentNumber", Convert.ToInt32(user.UserDocumentNumber));
con.Open();
result = cmd.ExecuteScalar().ToString();
message.Data = result.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
message.Data = e.Message;
}
finally
{
con.Close();
}
Class 获取连接:
public class MySettings
{
public string DbConnection { get; set; }
}
启动时:
services.Configure<MySettings>(Configuration.GetSection("ConnectionStrings"));
程序:
ALTER PROCEDURE [dbo].[SPUpdateUser]
(@UserId INTEGER,
@DateOfBirth DATE,
@UserNames NVARCHAR(30),
@UserLastNames NVARCHAR(30),
@UserPlatformName VARCHAR(30),
@UserPassword NVARCHAR(15),
@UserGender NVARCHAR(9),
@UserDocumentNumber INTEGER)
AS
BEGIN
SET NOCOUNT ON;
UPDATE Users
SET DateOfBirth = @DateOfBirth,
UserNames = @UserNames,
UserLastNames = @UserLastNames,
UserPlatformName = @UserPlatformName,
UserPassword = @UserPassword,
UserGender = @UserGender,
UserDocumentNumber = @UserDocumentNumber
WHERE
UserId = @UserId
END
在 Postman 中,我正在发送:
{
"userId": 1,
"dateOfBirth": "1980-1-1",
"userNames": "James",
"userLastNames": "Rodriguez",
"userPlatformName": "1",
"userPassword": "1234",
"userGender": "Masculino",
"userDocumentNumber": 1,
"relatives": []
}
这两个整数有它们的转换,所以...为什么会出现此错误?
我该如何解决这个问题?
项目编译成功,我在VS中没有看到错误
appsettings.json
"ConnectionStrings": {
"DevConnection": "Data Source=DESKTOP-ARVMMP2\SQLEXPRESS;Initial Catalog=PraxedesDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
ConfigurationManager
不适用于 ASP.NET 核心中的 appsettings.json
,您可以使用 IConfiguration
而不是 ConfigurationManager
,如下所示:
public class HomeController : Controller
{
private readonly IConfiguration Configuration;
public HomeController(IConfiguration Configuration)
{
this.Configuration = Configuration;
}
public async Task<IActionResult> Index()
{
var con = new SqlConnection(Configuration.GetConnectionString("DevConnection"));
return View();
}
}
我正在使用 VS 2019,SQL Server 2016,我尝试使用 ADO.NET 更新注册表,但出现错误:
ConnectionString 属性 尚未初始化
控制器:
如果我用这个:
con = new SqlConnection(ConfigurationManager.ConnectionStrings["DevConnection"].ConnectionString);
我收到错误:
Object reference not set to an instance of an object.'
如果我使用:
con = new SqlConnection(appSettings.Value.DbConnection);
我得到:
The ConnectionString property has not been initialized.
其余代码:
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(user.UserId));
cmd.Parameters.AddWithValue("@DateOfBirth", user.DateOfBirth);
cmd.Parameters.AddWithValue("@UserNames", user.UserNames);
cmd.Parameters.AddWithValue("@UserLastNames", user.UserLastNames);
cmd.Parameters.AddWithValue("@UserPlatformName", user.UserPlatformName);
cmd.Parameters.AddWithValue("@UserPassword", user.UserPassword);
cmd.Parameters.AddWithValue("@UserGender", user.UserGender);
cmd.Parameters.AddWithValue("@UserDocumentNumber", Convert.ToInt32(user.UserDocumentNumber));
con.Open();
result = cmd.ExecuteScalar().ToString();
message.Data = result.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
message.Data = e.Message;
}
finally
{
con.Close();
}
Class 获取连接:
public class MySettings
{
public string DbConnection { get; set; }
}
启动时:
services.Configure<MySettings>(Configuration.GetSection("ConnectionStrings"));
程序:
ALTER PROCEDURE [dbo].[SPUpdateUser]
(@UserId INTEGER,
@DateOfBirth DATE,
@UserNames NVARCHAR(30),
@UserLastNames NVARCHAR(30),
@UserPlatformName VARCHAR(30),
@UserPassword NVARCHAR(15),
@UserGender NVARCHAR(9),
@UserDocumentNumber INTEGER)
AS
BEGIN
SET NOCOUNT ON;
UPDATE Users
SET DateOfBirth = @DateOfBirth,
UserNames = @UserNames,
UserLastNames = @UserLastNames,
UserPlatformName = @UserPlatformName,
UserPassword = @UserPassword,
UserGender = @UserGender,
UserDocumentNumber = @UserDocumentNumber
WHERE
UserId = @UserId
END
在 Postman 中,我正在发送:
{
"userId": 1,
"dateOfBirth": "1980-1-1",
"userNames": "James",
"userLastNames": "Rodriguez",
"userPlatformName": "1",
"userPassword": "1234",
"userGender": "Masculino",
"userDocumentNumber": 1,
"relatives": []
}
这两个整数有它们的转换,所以...为什么会出现此错误?
我该如何解决这个问题?
项目编译成功,我在VS中没有看到错误
appsettings.json
"ConnectionStrings": {
"DevConnection": "Data Source=DESKTOP-ARVMMP2\SQLEXPRESS;Initial Catalog=PraxedesDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
ConfigurationManager
不适用于 ASP.NET 核心中的 appsettings.json
,您可以使用 IConfiguration
而不是 ConfigurationManager
,如下所示:
public class HomeController : Controller
{
private readonly IConfiguration Configuration;
public HomeController(IConfiguration Configuration)
{
this.Configuration = Configuration;
}
public async Task<IActionResult> Index()
{
var con = new SqlConnection(Configuration.GetConnectionString("DevConnection"));
return View();
}
}