gpt4 book ai didi

c# - Dapper:ConnectionString 属性尚未初始化

转载 作者:行者123 更新时间:2023-12-01 16:32:21 27 4
gpt4 key购买 nike

我第一次玩 Dapper。看起来是一个非常方便的小工具。但我遇到了一个问题。在下面的小控制台应用程序中,第一个方法按预期运行。然而,第二种方法返回此错误:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The ConnectionString property has not been initialized.

我可以颠倒方法的顺序并得到相同的结果。我总是在第二次调用时收到错误。不确定我做错了什么。我也尝试不使用 db.Close(),但得到了相同的结果。

无论调用第二个方法,错误都在这一行:

db.Open();

有什么想法吗?谢谢!

class Program
{
static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString());

static void Main(string[] args)
{
IEnumerable<Policy> policy1 = PolicySelectAll();
IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
}


public static IEnumerable<Policy> PolicySelectAll()
{
var sql = "SELECT * FROM Policy";
IEnumerable<Policy> policy;
using (db)
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}

public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
{
var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
IEnumerable<Policy> policy;
using (db)
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}
}

编辑后:

根据答案,这就是我解决问题的方法:

class Program
{
static string connectionString = ConfigurationManager.ConnectionStrings["DapperConnection"].ToString();

static void Main(string[] args)
{
IEnumerable<Policy> policy1 = PolicySelectAll();
IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
}

public static IDbConnection GetConnection()
{
return new SqlConnection(connectionString);
}

public static IEnumerable<Policy> PolicySelectAll()
{
IDbConnection db = GetConnection();
var sql = "SELECT * FROM Policy";
IEnumerable<Policy> policy;
using (db)
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}

public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
{
IDbConnection db = GetConnection();
var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
IEnumerable<Policy> policy;
using (db)
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}
}

最佳答案

如果您将 db 的定义移至方法范围内,那就没问题了。即:

class Program
{
static void Main(string[] args)
{
IEnumerable<Policy> policy1 = PolicySelectAll();
IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
}


public static IEnumerable<Policy> PolicySelectAll()
{
var sql = "SELECT * FROM Policy";
IEnumerable<Policy> policy;
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString()))
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}

public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
{
var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
IEnumerable<Policy> policy;
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString()))
{
db.Open();
policy = db.Query<Policy>(sql);
db.Close();
}
return policy;
}
}

}

关于c# - Dapper:ConnectionString 属性尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36156521/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com