如何将条形码图像保存到数据库 [英] how to save barcode image to database

查看:496
本文介绍了如何将条形码图像保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序想要将条形码图像保存到没有路径的数据库中。但有一个问题。我认为即使是条形码图像,图片框也没有任何价值..这是我的代码

my program wants to save the barcode image into database without path. but there is a problem. i think the picturebox dont have any value even if theres an barcode image.. this is my code

MemoryStream stream = new MemoryStream();
picBarcode.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] pic = stream.ToArray();
SqlCommand cmd = new SqlCommand("INSERT INTO tblproducts (Barcode,BarcodeImage) values ('" + txtBarcode.Text + "',@pic)", DDC_Con);
cmd.Parameters.AddWithValue("@Pic", pic);
try
{
    DDC_Con.Open();
    cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    DDC_Con.Close();
}

推荐答案

这就是我在项目中所做的。

上面的其他评论是正确的,当我尝试将图像添加到数据库时,我很快意识到这不是最好的解决方案。



This is what I did in my project.
The other comments above are correct, when I tried adding images to the database, I quickly realised it was not the best solution.

MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
byte[] imgbytes = ms.GetBuffer();
imgRow.ImageBytes = imgbytes;
this.imageTable.AddEventImageRow(imgRow);
nrows = this.imageAdapter.Update(this.imageTable);
Logging.LogMessage(nrows.ToString() + " image rows added");


这篇关于如何将条形码图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆