UPDATE命令返回null :( ..为什么? [英] UPDATE command returning null :(.. why?

查看:569
本文介绍了UPDATE命令返回null :( ..为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void GVImages_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            string ID = ((Label)GVImages.Rows[e.RowIndex].FindControl("lblID")).Text;

            string Name = ((TextBox)GVImages.Rows[e.RowIndex].FindControl("txtName")).Text;
            FileUpload f = ((FileUpload)GVImages.Rows[e.RowIndex].FindControl("FileUpload2"));
           
            byte[] imageBytes = null;
           
            if (f.HasFile)
            {
                //variable to store the image content
                imageBytes = new byte[f.PostedFile.ContentLength];
              
                HttpPostedFile uploadImage = f.PostedFile;
                //read the image stream from the post and store it in imageBytes
                uploadImage.InputStream.Read(imageBytes, 0, (int)f.PostedFile.ContentLength);
            }

            con.Open();
          MySqlCommand cmd = new MySqlCommand("Update images set Name=@Name,Content=@Content where ID="+ID, con);
      
            cmd.Parameters.Add("@ID", MySqlDbType.Int16).Value = ID;
  
            cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value= Name;

            cmd.Parameters.Add("@Content", MySqlDbType.LongBlob).Value = imageBytes;

            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            
            GVImages.EditIndex = -1;
            con.Close();
            databind();
      
        }

推荐答案

尝试替换

Try replaceing
MySqlCommand cmd = new MySqlCommand(" Update images set Name=@Name,Content=@Content where ID=" +ID, con);



with


with

MySqlCommand cmd = new MySqlCommand(" Update images set Name=@Name,Content=@Content where ID=@ID", con);


基本上,ExecuteNonQuery()方法应该返回一个整数,但你不能在代码中的任何变量中捕获它;您可以通过调试来观察其价值。



您应该关注的问题是:您希望从UPDATE命令获得什么?

您希望获得结果集(即一行或多行)吗?你不会。

如果你想从查询中得到一个结果集,它必须是一个SELECT。



所以,拜托,我们需要知道你对SQL请求的期望是什么。
Basically, ExecuteNonQuery() method should return you an integer, but you don''t catch it in any variable in your code ; you can watch its value by debugging, either.

The question you should concentrate on is : What do you want to get from an UPDATE command ?
Do you expect to get a resultset (i.e., one or more rows) ? You won''t.
If you want to get a resultset from a query, it must be a SELECT one.

So, please, we need to know what exactly you are expecting from your SQL request.


正如ThePhantomMenace指出的那样,你在串联ID上,但是后来又添加了一个名为@ID的参数。您需要将SQL命令文本更改为

更新图像集名称= @名称,内容= @内容ID = @ ID
As ThePhantomMenace pointed out, you are concatenating on the ID in the string but then are adding a parameter named @ID. You need to change the SQL command text to be
"Update images set Name=@Name,Content=@Content where ID=@ID"


这篇关于UPDATE命令返回null :( ..为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆