昨天晚上写一个小项目,用的.net2003+Access,写了一个针对新闻表的增删改类,添加,删除都测试通过.可是就卡在编辑上面. 因为的数据操作类是用SqlHelper修改的,所以先怀疑时不时那里有问题. 设了断点走了一圈,发现那里肯定没有问题,方法也正确,并且还不报错.当时真是感觉非常恼火.以下是当时的代码
/// <summary>
/// 更新一条新闻
/// </summary>
/// <returns></returns>
public bool Update()
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update News set ");
strSql.Append("NewTypeID_News=@NewTypeID_News,");
strSql.Append("Title_News=@Title_News,");
strSql.Append("Content_News=@Content_News,");
strSql.Append("ImgPath_News=@ImgPath_News");
strSql.Append(" where ID_News=@ID_News");
OleDbParameter[] parameters = {
new OleDbParameter("@ID_News", OleDbType.Integer,4),
new OleDbParameter("@NewTypeID_News", OleDbType.Integer,4),
new OleDbParameter("@Title_News", OleDbType.VarChar,255),
new OleDbParameter("@Content_News", OleDbType.LongVarChar),
new OleDbParameter("@ImgPath_News", OleDbType.VarChar,255)
};
parameters[0].Value = this.ID_News;
parameters[1].Value = this.NewTypeID_News;
parameters[2].Value = this.Title_News;
parameters[3].Value = this.Content_News;
parameters[4].Value = this.ImgPath_News;
return OLDBHelper.ExecuteNonQuery(strSql.ToString(),parameters);
}
经过几个小时的查资料,测试等等,仍然没有一点进展.当时已经接近崩溃的边缘. 突然发现代码里有一点可能引发问题的地方
/// <summary>
/// 更新一条新闻
/// </summary>
/// <returns></returns>
public bool Update()
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update News set ");
strSql.Append("NewTypeID_News=@NewTypeID_News,");
strSql.Append("Title_News=@Title_News,");
strSql.Append("Content_News=@Content_News,");
strSql.Append("ImgPath_News=@ImgPath_News");
strSql.Append(" where ID_News=@ID_News");
OleDbParameter[] parameters = {
new OleDbParameter("@ID_News", OleDbType.Integer,4),
new OleDbParameter("@NewTypeID_News", OleDbType.Integer,4),
new OleDbParameter("@Title_News", OleDbType.VarChar,255),
new OleDbParameter("@Content_News", OleDbType.LongVarChar),
new OleDbParameter("@ImgPath_News", OleDbType.VarChar,255)
};
parameters[0].Value = this.ID_News;
parameters[1].Value = this.NewTypeID_News;
parameters[2].Value = this.Title_News;
parameters[3].Value = this.Content_News;
parameters[4].Value = this.ImgPath_News;
return OLDBHelper.ExecuteNonQuery(strSql.ToString(),parameters);
}
在Sql语句里@ID_News是最后出现的,但是在parameters数组 里面 它是在第一个位置,我抱着死马当活马医的心理,把代码修改成下面样子
/// <summary>
/// 更新一条新闻
/// </summary>
/// <returns></returns>
public bool Update()
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update News set ");
strSql.Append("NewTypeID_News=@NewTypeID_News,");
strSql.Append("Title_News=@Title_News,");
strSql.Append("Content_News=@Content_News,");
strSql.Append("ImgPath_News=@ImgPath_News");
strSql.Append(" where ID_News=@ID_News");
OleDbParameter[] parameters = {
new OleDbParameter("@NewTypeID_News", OleDbType.Integer,4),
new OleDbParameter("@Title_News", OleDbType.VarChar,255),
new OleDbParameter("@Content_News", OleDbType.LongVarChar),
new OleDbParameter("@ImgPath_News", OleDbType.VarChar,255),
new OleDbParameter("@ID_News", OleDbType.Integer,4)
};
parameters[0].Value = this.NewTypeID_News;
parameters[1].Value = this.Title_News;
parameters[2].Value = this.Content_News;
parameters[3].Value = this.ImgPath_News;
parameters[4].Value = this.ID_News;
return OLDBHelper.ExecuteNonQuery(strSql.ToString(),parameters);
}
结果 测试马上通过.看着弹出来的编辑成功的对话框,自己当时有种要去炸掉微软的冲动,苍天啊,上帝啊,盖茨啊 还我那4个小时的时间