简单的代码即可存储和重新整理图像 [英] Simple code to Store and retieve image

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

问题描述

我有一张桌子,这是

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....

推荐答案


存储很容易:使用参数化查询,并且通过INSERT查询将图像的字节内容传递给数据库:
Store is easy: use a parametrized query, and hand the byte content of the image to the database via an INSERT query:
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);
                }
            }
        }
    }


看看
http://www.redmondpie.com/inserting -in-and-retrieving-image-from-sql-server-database-using-c/ [ http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 [ ^ ]
Have a look at
http://riteshshah.wordpress.com/2009/03/07/image-store-in-sql-server-2005-database-and-retrieve-it-in-aspnet-application-with-c/[^]
http://www.redmondpie.com/inserting-in-and-retrieving-image-from-sql-server-database-using-c/[^]
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^]


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

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