C#如何检查数据库中是否已存在Picturebox图像

时间:2018-09-16 06:56:10

标签: c# winforms sql-server-2008

在这些代码下面,我试图验证我的图片框图像是否已存在于数据库中。更准确地说,如果用户尝试插入相同的图像,它将验证“图像已经存在”

这是我得到的错误:

  

(参数化查询'(@Image varbinary(8000))从中选择COUNT(*)   employee_product wh'期望使用参数'@Image',而不是   提供。”

我在这里做错了什么?还是我忘记了什么? 我希望有人能够帮助我。谢谢

public partial class ADDProduct : MetroForm
{
    SIMSProduct _view;
    public ADDProduct(SIMSProduct _view)
    {
        InitializeComponent();
        this._view = _view;                 
    }
    DataTable dt = new DataTable();
    byte[] photobyte;
    string date = DateTime.Now.ToString("MMMM-dd-yyyy");
    public void ValidateImage(byte[] image)
    {
        using (var con = SQLConnection.GetConnection())
        {
                using (var select = new SqlCommand("Select COUNT(*) from employee_product where Image= @Image", con))
                {

                select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                    using (var sda = new SqlDataAdapter(select))
                    {
                        int count = (int)select.ExecuteScalar();
                        if (count > 0)
                        {
                            lbl_image.Show();
                        }
                    }
                }
        }          
    }
 private void btn_add_Click(object sender, EventArgs e)
    {
        _view.ID = txt_id.Text;
        using (var con = SQLConnection.GetConnection())
        {
            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text) || pictureBox1.Image == null )
            {
                CustomNotifcation.Show("Please input the required fields", CustomNotifcation.AlertType.warning);
            }
            else
            {
                ValidateItem.IsValidItem(txt_code, lbl_code);
                ValidateImage(photobyte);
                if (lbl_code.Visible == true)
                {
                    CustomNotifcation.Show("CODE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_code.Visible = false;
                }
                else if (lbl_image.Visible == true)
                {
                    CustomNotifcation.Show("IMAGE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_image.Visible = false;
                }
                else
                {
                    using (var select = new SqlCommand("Insert into employee_product (Image, ID, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image,@ID, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)", con))
                    {
                        var ms = new MemoryStream();
                        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                        photobyte = ms.GetBuffer();

                        select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                        select.Parameters.Add("@ID", SqlDbType.VarChar).Value = txt_id.Text;
                        select.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = cbox_supplier.Text;
                        select.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = txt_code.Text.Trim();
                        select.Parameters.Add("@Itemdescription", SqlDbType.VarChar).Value = txt_item.Text.Trim();
                        select.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
                        select.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text.Trim();
                        select.Parameters.Add("@Unitcost", SqlDbType.Int).Value = txt_cost.Text.Trim();
                        select.ExecuteNonQuery();
                        CustomMessage.Show("Message: Item successfully added!", CustomMessage.Messagetype.Success);
                        pictureBox1.Image = null;
                        cbox_supplier.Items.Clear();
                        txt_code.Clear();
                        txt_item.Clear();
                        txt_quantity.Clear();
                        txt_cost.Clear();
                        _view.btn_update.Enabled = false;
                        _view.AddingProduct();
                        this.Close();
                    }
                }                
            }                
        }
    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter = "Image Files (*.jpg;*.jpeg;.*.png;)|*.jpg;*.jpeg;.*.png;";
            ofd.FilterIndex = 1;
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(ofd.FileName);

            }
        }
    }
 }

1 个答案:

答案 0 :(得分:1)

您可以为文件(例如哈希或校验和)生成一些唯一值,并将其与字节流一起存储在DB中,这些字节流可用于检查文件是否存在。通常,这些机制不用于此目的。仅在文件内容完全相同的情况下才有效。即使只有很小的变化,也无法识别出匹配项。

您可以找到类似于字符串哈希的哈希:

using(SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
    hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
}

或者,您也可以像通常一样决定存储一些备用信息。例如文件名或基于用户的验证,以检查文件是否存在。