ASP.NET 中的 SqlConnection 问题

发布于 2024-12-05 23:18:58 字数 1842 浏览 5 评论 0原文

我创建了一个 3tier 应用程序..我想在其中调用连接数据库并相应更新记录的更新方法。

下面是我的数据库访问层。

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

下面是我的业务逻辑层,

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

当我创建 BusinessLogic 对象并调用更新方法时,它会按预期调用 DataLogic 的更新方法,但在更新数据库之前它会抛出错误,指出 ExecuteNonQuery 需要打开且可用的连接。但我已经打开了连接。

请任何人帮我解决有关 sqlconnection 的问题。

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 crossarticle_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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-12-12 23:18:58

问题是您正在打开两个单独的连接。请尝试以下操作:

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

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();
    }
}
霓裳挽歌倾城醉 2024-12-12 23:18:58

您的 ConnectDatabase 始终返回一个新的连接:

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

您应该使用局部变量并从工厂方法初始化一次:

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

例如:

using (SqlConnection con = CreateConnection())
{
   using (SqlCommand comUpdateArticle = new SqlCommand(updateString, con))
   {
      con.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();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文