本文介绍了简单的代码即可存储和重新整理图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,这是

StudentId int,
名称char(20),
Student_img图片
我想将图像的详细信息存储在数据库中,并使用Windows窗体检索它

提前thx....

I have a Table ilke this

StudentId int,
Name char(20),
Student_img image
I want to store details with image in database and retrive it using windows forms

thx in advance....

推荐答案


MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.ToArray();
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (StudentId, Name, Student_img) VALUES (@ID, @NM, @IM)", con))
        {
        com.Parameters.AddWithValue("@ID", Id);
        com.Parameters.AddWithValue("@NM", Name);
        com.Parameters.AddWithValue("@IM", bytes);
        com.ExecuteNonQuery();
        }
    }

检索不再困难:

Retrieve is no harder:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT StudentId, Name, Student_img FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["StudentId"];
                string desc = (string) reader["Name"];
                byte[] bytes = (byte[]) reader["Student_img"];
                MemoryStream ms = new MemoryStream(bytes);
                myImage = Image.FromStream(ms);
                }
            }
        }
    }



这篇关于简单的代码即可存储和重新整理图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:24