已有57人关注
相同的sqlcommand.ExecuteNonQuery()为什么返回的值不一样
发表在C#图书答疑 2011-03-05
是否精华
版块置顶:
请看下面代码,注意红色部分
 if (intFalg == 1) //intFlag是操作标记,1表示添加
                {
                    //调用jhMenthod类中的tb_JhGoodsInfoMenthodAdd方法,该方法向数据库中添加数据并返回int值
                    if ([font color=#FF0000]jhMenthod.tb_JhGoodsInfoMenthodAdd(jhGood)==2[/font])
                    {
                        MessageBox.Show("添加成功","提示");
                        intFalg = 0;
                        jhMenthod.tb_JhGoodsInfoFind("",5,dataGridView1);
                        ControlStatus();
                        ClearContorl();
                    }
                    else
                    {
                        ......                    }

                }
                if (intFalg == 2)
                {    //调用jhMinthod类中的tb_JhGoodsInfoMenthodUpdate函数
                    if ([font color=#FF0000]jhMenthod.tb_JhGoodsInfoMenthodUpdate(jhGood)==1[/font])
                    {
                        MessageBox.Show("修改成功", "提示");
                        intFalg = 0;
                        jhMenthod.tb_JhGoodsInfoFind("", 5, dataGridView1);
                        ControlStatus();
                        ClearContorl();
                    }
                    else
                    {
                        ......                    }
下面是调用的函数:
 public int tb_JhGoodsInfoMenthodAdd(tb_JhGoodsInfo tbGood) //向数据库中添加数据,并返回一个int值作为标记
       {
           int intFalg = 0;
           try
           {
               string str_Add = "insert into tb_JhGoodsInfo values( ";
               str_Add += "'" + tbGood.strGoodsID + "','" + tbGood.strEmpId + "','" + tbGood.strJhCompName + "',";
               str_Add += "'" + tbGood.strDepotName + "','" + tbGood.strGoodsName + "','" + tbGood.strGoodsNum + "',";
               str_Add += "'" + tbGood.strGoodsUnit + "'," + tbGood.deGoodsJhPrice + " ," + tbGood.deGoodsSellPrice + " ,";
               str_Add += "" + tbGood.deGoodsNeedPrice + "," + tbGood.deGoodsNoPrice + ",'" + tbGood.strGoodsRemark + "',";
               str_Add += "'" + tbGood.DaGoodTime + "'," + tbGood.Falg + ")";
               getSqlConnection getConnection = new getSqlConnection();
               conn = getConnection.GetCon();
               cmd = new SqlCommand(str_Add, conn);
               intFalg = cmd.ExecuteNonQuery(); // [font color=#FF0000]这里应该是2才对,可是明明只添加了一行为什么返回的是2?可下面同样的语句为什么返回的是1
[/font]               
               conn.Dispose();
               return intFalg;
这是另一个函数:
public int tb_JhGoodsInfoMenthodUpdate(tb_JhGoodsInfo tbGood)
       {
           int intFalg = 0;
           try
           {
               string str_Add = "update tb_JhGoodsInfo set ";
               str_Add += "EmpId='" + tbGood.strEmpId + "',JhCompName='" + tbGood.strJhCompName + "',";
               str_Add += "DepotName='" + tbGood.strDepotName + "',GoodsName='" + tbGood.strGoodsName + "',GoodsNum='" + tbGood.strGoodsNum + "',";
               str_Add += "GoodsUnit='" + tbGood.strGoodsUnit + "',GoodsJhPrice=" + tbGood.deGoodsJhPrice + " ,GoodsSellPrice=" + tbGood.deGoodsSellPrice + " ,";
               str_Add += "GoodsNeedPrice=" + tbGood.deGoodsNeedPrice + ",GoodsNoPrice=" + tbGood.deGoodsNoPrice + ",GoodsRemark='" + tbGood.strGoodsRemark + "',";
               str_Add += "GoodTime='" + tbGood.DaGoodTime + "',Falg='" + tbGood.Falg + "' where GoodsID ='" + tbGood.strGoodsID + "'";
               getSqlConnection getConnection = new getSqlConnection();
               conn = getConnection.GetCon();
               cmd = new SqlCommand(str_Add, conn);
               intFalg = cmd.ExecuteNonQuery();[font color=#FF0000]//这里cmd.ExecuteNonQuery()的值是1,可和上面的为什么不一样?
[/font]               conn.Dispose();
                      return intFalg;
请指教,谢谢!
               
分享到:
精彩评论 1
东方_mrkj
学分:0 LV1
2011-03-07
沙发
读者你好:
    因为在向“tb_JhGoodsInfo”表中插入数据时,会调用该表的触发器,更新另一个表的数据,所以返回值为2
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照