C#从数据库读取图像并将其保存到另一个数据库

时间:2018-08-27 14:50:27

标签: c# winforms sql-server-2008

下面的代码是我尝试将图像检索到我的数据库并将其保存到另一个数据库的地方。在第一个图像中,您将看到当我单击“确定”按钮时在运行时遇到的错误。在第二张图像中,您将看到我已经在检索图像中插入了数据,但是遇到了该错误(我看到该图像已插入,但是我不知道那是正确的图像)

在我的employee_product表中,“图像”列=图像数据类型 (此表是我检索图像的地方)

在我的product_result表中,我具有相同的列和相同的数据类型 (此表是我插入检索到的图像的地方)

我想要发生的是检索该图像,然后单击“确定”按钮,它将保存到我的数据库中,并且还会显示在我的datagridview中。

//My Code for Adding the image to my employee_product
private void btn_add_Click(object sender, EventArgs e)
    {
        using (var con = SQLConnection.GetConnection())
        {
            try
            {
                var ms = new MemoryStream();                    
                pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                byte[] PhotoByte = ms.ToArray();
                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))
                {
                    MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                else
                {
                    var selectCommand = new SqlCommand("Insert into employee_product (Image, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)", con);
                    selectCommand.Parameters.AddWithValue("@Image", PhotoByte);
                    selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
                    selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
                    selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
                    selectCommand.Parameters.AddWithValue("@Date", date);
                    selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
                    selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
                    selectCommand.ExecuteNonQuery();
                    MessageBox.Show("Added successfully", "SIMS", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txt_path.Clear();
                    pictureBox1.Image = null;
                    cbox_supplier.Items.Clear();
                    txt_code.Clear();
                    txt_item.Clear();
                    txt_quantity.Clear();
                    txt_cost.Clear();
                    _view.AddingProduct();

                }
              }
            catch (Exception)
            {
                MessageBox.Show("Input image");
            }
        } 

    }

byte[] data;
public void display()
    {
        using (var con = SQLConnection.GetConnection())
        {
            SqlCommand selectTable = new SqlCommand("Select * from product_result ; ", con);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = selectTable;
            dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bSource = new BindingSource();

            bSource.DataSource = dbdataset;
            _view.ListProduct.DataSource = bSource;
            sda.Update(dbdataset);
        }
    }

private void btn_ok_Click(object sender, EventArgs e)
    {        
        using (var con = SQLConnection.GetConnection())
        {

            var selects = new SqlCommand("Select * from employee_product where Codeitem =@Codeitem ", con);
            selects.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = _view.txt_code.Text;
            SqlDataReader reader;
            reader = selects.ExecuteReader();
            while (reader.Read())
            {

                data = (byte[])reader["Image"];


            }                
            reader.Close();
            var selectCommand = new SqlCommand("Insert into product_result (Image, Code, Name, Price, Discount, Quantity) Values (@Image, @Code, @Name, @Price, @Discount, @Quantity)", con);
            selectCommand.Parameters.AddWithValue("@Image", data);
            selectCommand.Parameters.AddWithValue("@Code", _view.txt_code.Text);
            selectCommand.Parameters.AddWithValue("@Name", _view.txt_name.Text.Trim());
            selectCommand.Parameters.AddWithValue("@Price", _view.txt_price.Text.Trim());
            selectCommand.Parameters.AddWithValue("@Discount", txt_discount.Text.Trim());
            selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
            selectCommand.ExecuteNonQuery();
            var select = new SqlCommand("Update employee_product set quantity = quantity - @Quantity where Codeitem= @Codeitem",con);
            select.Parameters.AddWithValue("@Codeitem", _view.txt_code.Text);
            select.Parameters.AddWithValue("@Quantity", txt_quantity.Text);
            select.ExecuteNonQuery();
            this.Close();
            display();
        }
    }

enter image description here

您将在此图片中看到该图像具有代码。如果用户输入其他代码,它将获得另一张图像 enter image description here

2 个答案:

答案 0 :(得分:0)

您将需要提供一种单元格格式化方法,该方法将数据放入数据网格视图可以使用的图像对象中。

private void dgv_CellFormatting(object sender, 
DataGridViewCellFormattingEventArgs e)
{
    if (dgv.Columns[e.ColumnIndex].Name == "Image")
    {
        if (e.Value != null)
        {
            ... convert byte[] to image...
        }
    }
 }

答案 1 :(得分:0)

您可以像这样将PictureBox的图像保存到db。...

MemoryStream getimagestream = new MemoryStream();
pictureBox1.Image.Save(getimagestream, _pb_photo.Image.RawFormat);
byte[] Imagedata = getimagestream.GetBuffer();

您可以像这样从db接收图片到图片框。

var ImagedataReceive = (byte[])reader["Image"];
var stream = new MemoryStream(ImagedataReceive);
pictureBox1.Image = Image.FromStream(stream);