| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1222 人关注过本帖
标题:C#从数据库读图片的问题
只看楼主 加入收藏
lq_2008
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-9-24
收藏
 问题点数:0 回复次数:2 
C#从数据库读图片的问题
我以sqlserver2K中northwind数据库中categories表为例对里面的图片读取,但运行不能读出来.

protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("server=(local);database=northwind;user id=sa;pwd=");
        connection.Open();
        SqlCommand comm = new SqlCommand("select * from categories", connection);
        SqlDataReader reader = comm.ExecuteReader();
        while (reader.Read())
        {
            Response.ContentType ="image/jpeg";
            Response.BinaryWrite((byte[])reader["Picture"]);  
        }
    }
搜索更多相关主题的帖子: 数据库 connection reader SqlCommand 
2008-01-03 13:52
kem173806992
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-19
收藏
得分:0 
/*将PictureBox中的影像以JPEG图形格式存入MemoryStream对象中
                     */
                    MemoryStream ms = new MemoryStream();
                    
                    Bitmap bit = new Bitmap(pictureBox1.Image);
                    bit.Save(ms, ImageFormat.Jpeg);

                    byte[] Image = new byte[(int)ms.Length];

                    ms.Position = 0;
                    ms.Read(Image, 0, (int)ms.Length);
                    ms.Close();

                    /* @ 声明的变量赋值
                     * 要用Parameters集合...
                     * 第一个参数写参数名..第二个写值的数据库类型..第三个写值的数据库长度..
                     */
                    cmd.Parameters.Add("@tupian", SqlDbType.Image, Image.Length).Value = Image;
                    //cmd是SQL命令语句
                    //执行SqlCommand的SQL语句
                    cmd.ExecuteNonQuery();

                    getlist();  //刷新下拉框
                    setClear(); //清空文本框
                    MessageBox.Show("保存成功");
2008-01-04 17:44
kem173806992
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-19
收藏
得分:0 
这是一种比较麻烦的方法....还有一种..不过没时间..哈哈..
先用这种吧...看不明白就没办法...
2008-01-04 17:45
快速回复:C#从数据库读图片的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012095 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved