开发者

SqlConnection Problems in asp.net

i have created a 3tier application.. where i want to call the update method which connect the database and update the records accordingly.

below is my database access layer.

public class DataLogic
{
    public DataLogic()
    {
    }

    public SqlConnection ConnectDatabase
    {
        get { return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString); }
    }

    public int UpdateArticle(BusinessLogic b, int ArticleId)
    {
        int updateExecuted = -1;
        StringBuilder formParamString = new StringBuilder();
        formParamString.Append("IsArticlePaging=" + b.IsPagingEnable + " ");
        string updateString = "update cro开发者_运维知识库ssarticle_article set " + formParamString.ToString() + "where id = " + ArticleId + "";
        try
        {
            using (SqlCommand comUpdateArticle = new SqlCommand(updateString, ConnectDatabase))
            {
                ConnectDatabase.Open();
                updateExecuted = comUpdateArticle.ExecuteNonQuery();
            }
        }
        catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); }
        finally { ConnectDatabase.Close(); }
        return updateExecuted;
    }
}

below is my business logic layer

public class BusinessLogic
{
    DataLogic dLogic = new DataLogic();
    public BusinessLogic()
    {

    }
    private bool _IsPagingEnable;

    public bool IsPagingEnable
    {
        get { return _IsPagingEnable; }
        set { _IsPagingEnable = value; }
    }
    private int _articleID;
    public int ArticleID
    {
        get { return _articleID; }
        set { _articleID = value; }
    }

    public int UpdateExtraFieldArticle()
    {
        return dLogic.UpdateArticle(this, ArticleID);
    }
}

now when i create the BusinessLogic object and call the update method, it calls the DataLogic's update method as expected, but before updating the database it throws error saying, ExecuteNonQuery requires open and available connection. but i have already opened the connection.

Please any one help me regarding the sqlconnection.


The problem is you are opening two separate connections. Try the following:

using (sqlConnection connection = ConnectDatabase)
{
    using (SqlCommand comUpdateArticle = new SqlCommand(updateString, connection))
    {
        connection.Open();
        updateExecuted = comUpdateArticle.ExecuteNonQuery();
    }
}


Your ConnectDatabase always returns a new Connection:

public SqlConnection ConnectDatabase
{
 get { return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString); }
}

You should use a local variable and initialize it once from a factory-method:

public SqlConnection CreateConnection
{
    return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
}

For example:

using (SqlConnection con = CreateConnection())
{
   using (SqlCommand comUpdateArticle = new SqlCommand(updateString, con))
   {
      con.Open();
      updateExecuted = comUpdateArticle.ExecuteNonQuery();
   }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜