这些资料是我学习的时候在网络上寻找到的,发布出来希望对大家有帮助,一起努力前进,嘿嘿......Microsoft C#规范 2.0 版 GFC用户提交

feedsky 抓虾 pageflakes google reader my yahoo bloglines 鲜果 有道 http://wap.feedsky.com/bliplink

c# 图片(压缩文件,exe)等存储到数据库中原理


1.首先将图片生成一个数据流

2.将数据流生成为一个字节数组

3.创建数据库连接

4.创建insert Sql 语句将数据流插入到数据库中

如过想显示图片,可以逆向来做.下面的代码只是一个验证可以实现的,并不是很好的代码.希望可以帮助大家理解
        private void button2_Click(object sender, EventArgs e)
        {//存储图片的过程
            OpenFileDialog dl = new OpenFileDialog();
            dl.Filter = "(*.jgp)|*.jpg|(*.gif)|*.gif";
            if (dl.ShowDialog() == DialogResult.OK)
            {
                Image ig = Image.FromFile(dl.FileName);//通过文件获取图片
                MemoryStream ms = new MemoryStream();
                ig.Save(ms, ImageFormat.Gif);//将图片存为数据流(这里是内存流)
                byte[] bt = ms.ToArray();//将流存为自节数据
                SqlConnection con = new SqlConnection("Data Source=A652AB386E04418\\SQLEXPRESS;Initial Catalog=Pic;Integrated Security=True ");
                SqlCommand com = new SqlCommand("insert into Pic(pic) values(@pic)", con);
                SqlParameter par = new SqlParameter("@pic", SqlDbType.Binary);
                par.Value = bt;//将字节数组复制给参数
                com.Parameters.Add(par);
                con.Open();
                com.ExecuteNonQuery();//执行命令
                con.Close();
                MessageBox.Show("successful");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=A652AB386E04418\\SQLEXPRESS;Initial Catalog=Pic;Integrated Security=True ");
            SqlCommand com = new SqlCommand("select pic from Pic where ID=2", con);
            con.Open();
            SqlDataReader sdr = com.ExecuteReader();
            byte[] b=null ;
            while (sdr.Read())
            {
                 b=(sdr[0] as byte []);//读为字节数组
            }
            con.Close();
            MemoryStream ms = new MemoryStream(b);//通过数组来实例一个内存流对象
            Image ig = Image.FromStream(ms);//通过流来生成一个图片实例
            pictureBox1.Image = ig;//显示图片


友情链接

郑州大学软件学院 SpringWidgets-Blogger 徵信社 翻译公司 链接帮手网 行驶证字体 酷站目录 Friend Connectified