Dapper:ConnectionString属性尚未初始化

时间:2016-03-22 14:02:44

标签: c# sql .net dapper

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

  

System.Data.dll中发生了未处理的“System.InvalidOperationException”类型异常       附加信息:ConnectionString属性尚未初始化。

我可以改变方法的顺序并得到相同的结果。它总是在第二次调用时得到错误。不知道我做错了什么。我也试过不使用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;
    }
}

1 个答案:

答案 0 :(得分:2)

如果将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;
    }
}

}