com.ExecuteNonQuery()返回结果为-1

dan810103 2013-03-16 09:16:37
c#code
public static SqlConnection createCon()
{
con = new SqlConnection("server=ILSH0QYY4HVJMZX; database=db_WL;Integrated Security=True;");
return con;
}
public static bool execSQL(string sql)
{
SqlConnection con = createCon();
con.Open();
SqlCommand com = new SqlCommand(sql, con);
try
{

int i= com.ExecuteNonQuery();//执行update语句后i=-1

}
catch (Exception ex)
{
con.Close();
return false;
}
return true;
}
protected void gridviewGoodInfo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int ID =Convert.ToInt32(this.gridviewGoodInfo.DataKeys[e.NewSelectedIndex].Value.ToString());
string sql = string.Format("select Auditing from tb_Freight where ID={0}", ID);
SqlDataReader dr = DataOperate.getRow(sql);
dr.Read();
int Auditing=Convert.ToInt32(dr["Auditing"]);
if (Auditing == 0)
{
Auditing = 1;
}
else
{
Auditing = 0;
}
string updateSQL = string.Format("update tb_Freight set Auditing='{0}' where ID={1}",Convert.ToString(Auditing), ID);
DataOperate.execSQL(sql);
}
...全文
220 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2013-03-16
  • 打赏
  • 举报
回复
写程序要非常小心。。。 接分。。。
dan810103 2013-03-16
  • 打赏
  • 举报
回复
错误解决了, DataOperate.execSQL(sql);参数不应该是sql,而是updateSQL 。粗心。
dan810103 2013-03-16
  • 打赏
  • 举报
回复
没执行catch{},执行的是try{}语句,返回-1。(另:当execSQL()被其他方法调用时正确执行)
EnForGrass 2013-03-16
  • 打赏
  • 举报
回复
catch (Exception ex) { con.Close(); return false; } 看一下抛的什么异常
dan810103 2013-03-16
  • 打赏
  • 举报
回复
sql语句是正确的可以执行,但就是执行execSQL()方法时,com.ExecuteNonQuery()返回-1?
EnForGrass 2013-03-16
  • 打赏
  • 举报
回复
调试updateSQL 语句,拿到查询分析器执行一下看能成功执行吗

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧