Dapper: The ConnectionString property has not been initialized

.net c# dapper sql

Question

I'm playing around with Dapper for the first time. Looks like a pretty handy little tool. But I'm running into one problem. In the little console app below, the first method runs as expected. However the second method returns this error:

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

I can turn the order of the methods around and get the same results. It's always on the second call that I get the error. Not sure what I'm doing wrong. I also tried not using the db.Close(), but I got the same result.

The error is on this line in whatever method is called second:

db.Open();

Any ideas? Thanks!

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

EDIT AFTER:

Based on the answers, this is how I solved it:

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

Accepted Answer

if you move your definition of db into the method scope it will be fine. 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;
    }
}

}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why