0

我正在尝试创建包含

Image Id (int)
Imagename (varchar(50))
Image (image)

并在 aspx 中写入上传按钮此代码:

protected void btnUpload_Click(object sender, EventArgs e)
{
    //Condition to check if the file uploaded or not
    if (fileuploadImage.HasFile)
    {
        //getting length of uploaded file
        int length = fileuploadImage.PostedFile.ContentLength;

        //create a byte array to store the binary image data
        byte[] imgbyte = new byte[length];

        //store the currently selected file in memeory
        HttpPostedFile img = fileuploadImage.PostedFile;

        //set the binary data
        img.InputStream.Read(imgbyte, 0, length);

        string imagename = txtImageName.Text;

        //use the web.config to store the connection string
        SqlConnection connection = new SqlConnection(strcon);
        connection.Open();

        SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection);
       cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
       cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;

       int count = cmd.ExecuteNonQuery();
       connection.Close();

       if (count == 1)
       {
           BindGridData();
           txtImageName.Text = string.Empty;
           ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
       }
    }
}

当我上传新图像时,我需要首先检查该图像是否已存在于数据库中,如果不存在则将其保存在数据库中。

请问我该怎么做?

4

3 回答 3

1

添加负责检查文件名是否已存在于表中的方法。

    private bool FileExists(string imageName)
    {
        using (SqlConnection conn = new SqlConnection()) // establish connection
        {
            using (SqlCommand cmd =
                new SqlCommand("select 1 where exists(select Id from Image where ImageName = @)", conn))
            {
                cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imageName;
                return cmd.ExecuteNonQuery() > 0;
            }
        }
    }

然后我会这样称呼

        if (fileuploadImage.HasFile && !FileExists(txtImageName.Text))
        {
            ...
于 2012-10-02T15:35:39.743 回答
0
 string cmd ="if not exists (select * from Image where ImageName= @imagename); ";
 \\if you want to check image data
 \\ (select * from Image where SUBSTRING(ImageName, 1, 8000)= SUBSTRING(@imagename, 1, 8000) ); 
 string cmd += "INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)"; 
 SqlCommand cmd = new SqlCommand(cmd, connection);

如果要验证图像数据,可以尝试将其DATALENGTH用作两个图像的第一行检查。如果DATALENGTH不同,那么你假设有一个“不同的”图片“。
您也可以使用SUBSTRING(Image, 1, 8000)检查前 8000 个字节。
SUBSTRING(Image, DATALENGTH(Image) - 7999, 8000)可以检查最后 8000 个字节。

于 2012-10-02T14:47:44.700 回答
0

最快的方法之一是执行 UPDATE,如果 update 没有返回更新,则执行 INSERT。

string cmd = @"UPDATE Image SET Image = @imagedata WHERE ImageName = @ImageName
               IF @@ROWCOUNT=0
                  INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)";

或者如果查询图像本身:

string cmd = @"UPDATE Image SET ImageName = @ImageName WHERE Image = @imagedata
               IF @@ROWCOUNT=0
                  INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)";
于 2012-10-02T14:49:01.237 回答