开发者

executeScalar keeps returning exception errors

I am using the 'using' construct but when I load images rapidly in a loop, I get the following error:

ExecuteScalar requires an open and available Connection. The connection's current state is connecting.

The strange part is that it happens at different times in the loop every time I run it.

I have searched my entire solution and there are no calls to Open() or Close() anywhere but in the following:

    public myMethod()
    {
        string conString;
        conString = "Server=(local);Database=myDB;Uid=appaccess;Pwd=xxxxxx";
        con = new SqlConnection(conString);
        con.Open();
    }

    public void Dispose()
    {
        con.Close();
    }

And then later in my code I call this method in a loop of imgIDs:

    public byte[] GetImageBitStream(int imgID)
    {
        SqlCommand cmd = new SqlCommand("GetImageBitStream", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@imgID", imgID));
        Object picData = new Object();
        picData = cmd.Execut开发者_JS百科eScalar();
        if (picData == null)
        {
            picData = "";
        }
        return (byte[])picData;
    }

I don't know what to try next! This code used to work and recently stopped working when I switched to the 'using' format. But even if I switch back, it errors now.


Sounds like an connection leak to me.

Try refactoring along the following lines:

string conString;
conString = "Server=(local);Database=myDB;Uid=appaccess;Pwd=xxxxxx";
using (var con = new SqlConnection(conString))
{
     con.Open();
     foreach (int id in imgIdCollection)
     {
         var img = GetImageBitStream(id,con)
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜