上传新文件首先检查该文件是否已存在于数据库中,如果不存在则保存在数据库中

时间:2012-10-02 14:38:01

标签: c# sql-server

我正在尝试创建包含

的sql数据库
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);
       }
    }
}

当我上传新图片时,我需要首先检查这个图片是否已经存在于数据库中,如果它不存在,请将其保存在数据库中。

请问我该怎么做?

3 个答案:

答案 0 :(得分: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))
        {
            ...

答案 1 :(得分: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);

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

答案 2 :(得分:0)

最快的方法之一是进行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)";