ASP.NET 中的 SqlConnection 问题
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在打开两个单独的连接。请尝试以下操作:
The problem is you are opening two separate connections. Try the following:
您的
ConnectDatabase
始终返回一个新的连接:您应该使用局部变量并从工厂方法初始化一次:
例如:
Your
ConnectDatabase
always returns a new Connection:You should use a local variable and initialize it once from a factory-method:
For example: