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

我第一次和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的定义移动到方法范围中,那就没问题了. I.E.

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;
    }
}

}

点赞