ISNULL不在UPDATE语句中工作

时间:2014-05-16 01:49:23

标签: c# asp.net sql

我是编码的新手,对于任何滥用条款和我缺乏理解感到抱歉。

因此,更新查询中的ISNULL可能不是问题,正如我所说,我是新手。

基本上,我正在为作业创建一个简报网站。作者可以上传文章,但编辑者必须授权上传。这是我遇到麻烦的页面。除Image对象外,每个字段都更新。它保存了由作者上传的图像的路径。如果我要编辑任何字段,例如标题字段并更新数据库,则Image字段中的数据将被删除并替换为“NULL”。现在我显然不想要这个。如果在编辑页面中没有上传图像,我希望保留该值。如果我要选择一个新图像,它会完全更新,问题在于没有选择图像。我希望有人可以帮助我解决这个问题,因为它一直困扰着我,这可能是一件简单的事情,并希望得到任何帮助。

下面是我的UPDATE语句背后的代码,我相信这就是问题所在。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class admin_updatenews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void displayedit_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
    {
        info.Text = "Item Updated";

        FileUpload fileupdate = displayedit.EditItem.FindControl("imageupdate") as FileUpload;

        Label recordid = displayedit.EditItem.FindControl("idlabel1") as Label;
        Int32 id = Convert.ToInt32(recordid.Text);

        if (fileupdate.HasFile)
        {
            String fupload = fileupdate.FileName;

            Random r = new Random();
            int rInt = r.Next(0, 10000);

            String imgpath = "../images/" + rInt + fupload;

            fileupdate.SaveAs(Server.MapPath(imgpath));

            String newimage = rInt + fupload;

            string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
            SqlConnection myConnection = new SqlConnection(newsconnection);

            //myConnection.ConnectionString is now set to connectionString.
            myConnection.Open();

            String query = "UPDATE News SET postimage = ISNULL('" + newimage + "', postimage), Image = ISNULL('" + newimage + "', Image) WHERE id='" + id + "'";

            SqlCommand myCommand = new SqlCommand(query, myConnection);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
    protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        info.Text = "I am editing";
    }
    protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        info.Text = "Not Updating";
    }
} 

由于我不善于解释所有条款,因此我会提供我的流程图片,希望能让事情变得清晰。

Inserted data

enter image description here

Inserted Data displayed

enter image description here

Edit Inserted Data

enter image description here

Update Inserted Data (No image)

enter image description here

Updated Database

enter image description here

2 个答案:

答案 0 :(得分:1)

ISNULL('" + newimage + "', NULL)毫无意义 - 你总是看到一个字符串常量是NULL,它永远不会是真的。

我怀疑你想要:

"UPDATE News SET postimage = " + newImage == null ? "null" : "'" + newimage + "'," ...

但是出于几个原因,您应该使用参数:

String query = "UPDATE News SET postimage = @postImage, Image = @Image WHERE id=@id";

SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@postImage",postImage ?? DBNull.Value);
myCommand.Parameters.AddWithValue("@Image",Image ?? DBNull.Value);
myCommand.Parameters.AddWithValue("@id",id);


myCommand.ExecuteNonQuery();
myConnection.Close();

请注意,如果postImageImagenull,则使用DBNull.Value代替null

答案 1 :(得分:0)

在你的例子中," newimage"将始终具有指向该文件的字符串值。因此,无需检查null,ISNULL表达式将不执行任何操作。您的图像暗示其他代码更新数据包括" postimage"和"图像"。如果你在其他代码中有类似的ISNULL表达式,我的猜测是你需要以下内容来确保如果你的新的" newimage"值为null:

String query = "UPDATE News SET postimage = ISNULL('" + newimage + "', postimage), Image = ISNULL('" + newimage + "', Image) WHERE id='" + id + "'";

这样" ISNULL"表达式说:"如果新值为null,则设置为当前值"。

相关问题