ConnectionString 属性 初始化
The ConnectionString property initialization
虽然这个问题看起来已经有了答案,但我的情况不同,下面是方法。
第一次成功,但后续请求失败。
我在主 class 中创建连接,并将其作为构造函数中的依赖项传递给数据库 class,并且每次调用都会重复使用它。
public class DB
{
private SqlConnection conn;
public DB(SqlConnection conn)
{
this.conn = conn;
}
public List<Records> GetRecords()
{
using (conn){
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Records", conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Records> rows = new List<Records>();
while (reader.Read())
{
rows.Add(new Records(reader.GetString(1)));
}
return rows;
}
}
}
}
来电者class
string connection = $@"
Data Source=;
Initial Catalog=;
Persist Security Info=True;
User ID={env["DATABASE_USER"]};
Password={env["DATABASE_PASSWORD"]};";
Db db = new DB(new SqlConnection(connection));
db.GetRecords();
fail:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request. System.InvalidOperationException: The ConnectionString property has
not been initialized.
我不是 100% 确定,但我想问题出在
using(conn)
当using关闭时,SqlConnection将被释放。
所以当你再次打电话时 db.GetRecords();,
conn.Open() 未初始化。 -> 异常
您不应将 SQLConnection
用作字段,而应用作方法内的局部变量。更改 class 以在其参数内接收连接字符串而不是 SqlConnection
的实例,并在使用它的任何方法中对其进行初始化:
public class DB
{
private string connectionString;
public DB(string connectionString)
{
this.connectionString = connectionString;
}
public List<Records> GetRecords()
{
using (var conn = new SqlConnection(connectionString)){
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Records", conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Records> rows = new List<Records>();
while (reader.Read())
{
rows.Add(new Records(reader.GetString(1)));
}
return rows;
}
}
}
}
更多详情,read this.
虽然这个问题看起来已经有了答案,但我的情况不同,下面是方法。
第一次成功,但后续请求失败。
我在主 class 中创建连接,并将其作为构造函数中的依赖项传递给数据库 class,并且每次调用都会重复使用它。
public class DB
{
private SqlConnection conn;
public DB(SqlConnection conn)
{
this.conn = conn;
}
public List<Records> GetRecords()
{
using (conn){
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Records", conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Records> rows = new List<Records>();
while (reader.Read())
{
rows.Add(new Records(reader.GetString(1)));
}
return rows;
}
}
}
}
来电者class
string connection = $@"
Data Source=;
Initial Catalog=;
Persist Security Info=True;
User ID={env["DATABASE_USER"]};
Password={env["DATABASE_PASSWORD"]};";
Db db = new DB(new SqlConnection(connection));
db.GetRecords();
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: The ConnectionString property has not been initialized.
我不是 100% 确定,但我想问题出在
using(conn)
当using关闭时,SqlConnection将被释放。
所以当你再次打电话时 db.GetRecords();,
conn.Open() 未初始化。 -> 异常
您不应将 SQLConnection
用作字段,而应用作方法内的局部变量。更改 class 以在其参数内接收连接字符串而不是 SqlConnection
的实例,并在使用它的任何方法中对其进行初始化:
public class DB
{
private string connectionString;
public DB(string connectionString)
{
this.connectionString = connectionString;
}
public List<Records> GetRecords()
{
using (var conn = new SqlConnection(connectionString)){
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Records", conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Records> rows = new List<Records>();
while (reader.Read())
{
rows.Add(new Records(reader.GetString(1)));
}
return rows;
}
}
}
}
更多详情,read this.