c# - C#如何检查Picturebox图像是否已存在于数据库中
问题描述
在这些代码下面,我试图验证我的图片框图像是否已经存在于数据库中。更准确地说,如果用户尝试插入相同的图像,它将验证“图像已经存在”
这是我得到的错误:
(参数化查询 '(@Image varbinary(8000))Select COUNT(*) from 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);
}
}
}
}
解决方案
您可以为文件生成一些唯一值,如哈希或校验和,并将其与字节流一起存储在 DB 中,可用于检查文件是否存在。通常这些机制不用于此目的。这仅适用于文件内容完全相同的情况。即使是最轻微的变化也将无法识别匹配。
你可以找到像字符串哈希这样的哈希:
using(SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
}
或者,您可以决定像我们通常那样存储一些备用信息。像文件名或基于用户的验证来检查文件是否存在。
推荐阅读
- c# - 实施自定义的基于令牌的授权/认证系统
- google-cloud-functions - 使用共享 VPC 的 Google 云数据流/功能
- python - 转换为小写:每个字母都被标记化
- python - TypeError: set_cookie() got an unexpected keyword argument 'samesite' (flask)
- java - 当它只是一个背景时,我应该把我的刷卡管理器放在哪里?
- python - 第二次更改海龟背景图像会导致海龟窗口无响应
- php - 比较 MySQL 和 CSV 并找出差异
- batch-file - netsh 显示没有其他内容的 wlan 配置文件
- javascript - 通过另一个对象数组过滤对象数组
- c# - 有没有办法在 C# 中通过引用一致地对对象进行排序/排序?