从数据库读取图片,用SqlDataReader读取精确到字符段 读的数据要转化成string类型。急等!!!! C#

时间:2021-02-11 14:51:50
从数据库读取图片,用SqlDataReader读取精确到字符段,比如数据库里的照片字段是zp,myredader要加zp字段不要用数字。读完后要在picturebox里显示。还要把myredader读出的字段转化成string类型。请各位帮帮忙,急等!!!!

7 个解决方案

#1


转化成string涉及到编码问题,直接读byte[]不行吗?

#2


如何读出图片呢?

#3


给你一个在web下读取图片的例子。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    private string connectionString = "server=wsmall\\sql2005; uid=sa; pwd=wsmall2; database=TestDB";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //读
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (fuTest.HasFile)
        {
            //获取文件后缀名
            string exName = Path.GetExtension(fuTest.PostedFile.FileName);
            //获取文件流
            Stream stream = fuTest.PostedFile.InputStream;
            //创建字节数组准备保存文件内容
            byte [] buffer = new byte[fuTest.PostedFile.ContentLength];
            //将文件内容读入到字节数组中
            stream.Read(buffer, 0, fuTest.PostedFile.ContentLength);

            SqlConnection myConn = new SqlConnection(connectionString);
            myConn.Open();
            string sql = "insert into ImageTest values(@length, @name, @content)";
            SqlParameter par = new SqlParameter("@content", SqlDbType.Image);
            par.Value = buffer;
            SqlParameter parName = new SqlParameter("@name", exName);
            SqlParameter parLength = new SqlParameter("@length", fuTest.PostedFile.ContentLength);

            SqlCommand myCmd = new SqlCommand(sql, myConn);
            myCmd.Parameters.Add(par);
            myCmd.Parameters.Add(parName);
            myCmd.Parameters.Add(parLength);


            myCmd.ExecuteNonQuery();
            myConn.Close();
            
        }
    }
    //取
    protected void btnRead_Click(object sender, EventArgs e)
    {
        string sql = "select * from imageTest where imageid = 1";

        SqlConnection myConn = new SqlConnection(connectionString);
        myConn.Open();
        SqlCommand myCmd = new SqlCommand(sql, myConn);
        SqlDataReader reader = myCmd.ExecuteReader();
        reader.Read();
        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
        //如果实要是用字段:
        //fileName += reader["zd"];
        fileName += reader.GetValue(0).ToString();

        FileStream stream = new FileStream(Server.MapPath("tmp\\" + fileName), FileMode.Create, FileAccess.Write);
        long length = reader.GetInt64(1);
        byte[] buffer = new byte[length];

        long test =  reader.GetBytes(3, 0, buffer, 0, buffer.Length);

        stream.Write(buffer, 0, (int)test);

        stream.Flush();
        stream.Close();


        Image1.ImageUrl = "~/tmp/" + fileName;
       
    }
}

#4


补充,数据库表的结构为
ImageID 自动增长列
ImageContent image --二进制字段
ImageLength --图片长度

#5


引用 4 楼 wsmall1 的回复:
补充,数据库表的结构为 
ImageID 自动增长列 
ImageContent image --二进制字段 
ImageLength --图片长度


不好意思,没有 ImageID 自动增长列  这个字段
应该为
ImageContent image --二进制字段 
ImageLength --图片长度

#6


up

#7


SqlDataReader myreader = objcmd.ExecuteReader();
            if (myreader.Read())
            {
                   里面如何写,请大家解决一下。
                      要求:先要读出(myread)[zp]字段,然后转化成string类型,给一个string字符串赋值。还有在picturebox接收图片显示。
                      代码,写一下,谢谢各位大虾。
            }

#1


转化成string涉及到编码问题,直接读byte[]不行吗?

#2


如何读出图片呢?

#3


给你一个在web下读取图片的例子。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    private string connectionString = "server=wsmall\\sql2005; uid=sa; pwd=wsmall2; database=TestDB";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //读
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (fuTest.HasFile)
        {
            //获取文件后缀名
            string exName = Path.GetExtension(fuTest.PostedFile.FileName);
            //获取文件流
            Stream stream = fuTest.PostedFile.InputStream;
            //创建字节数组准备保存文件内容
            byte [] buffer = new byte[fuTest.PostedFile.ContentLength];
            //将文件内容读入到字节数组中
            stream.Read(buffer, 0, fuTest.PostedFile.ContentLength);

            SqlConnection myConn = new SqlConnection(connectionString);
            myConn.Open();
            string sql = "insert into ImageTest values(@length, @name, @content)";
            SqlParameter par = new SqlParameter("@content", SqlDbType.Image);
            par.Value = buffer;
            SqlParameter parName = new SqlParameter("@name", exName);
            SqlParameter parLength = new SqlParameter("@length", fuTest.PostedFile.ContentLength);

            SqlCommand myCmd = new SqlCommand(sql, myConn);
            myCmd.Parameters.Add(par);
            myCmd.Parameters.Add(parName);
            myCmd.Parameters.Add(parLength);


            myCmd.ExecuteNonQuery();
            myConn.Close();
            
        }
    }
    //取
    protected void btnRead_Click(object sender, EventArgs e)
    {
        string sql = "select * from imageTest where imageid = 1";

        SqlConnection myConn = new SqlConnection(connectionString);
        myConn.Open();
        SqlCommand myCmd = new SqlCommand(sql, myConn);
        SqlDataReader reader = myCmd.ExecuteReader();
        reader.Read();
        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
        //如果实要是用字段:
        //fileName += reader["zd"];
        fileName += reader.GetValue(0).ToString();

        FileStream stream = new FileStream(Server.MapPath("tmp\\" + fileName), FileMode.Create, FileAccess.Write);
        long length = reader.GetInt64(1);
        byte[] buffer = new byte[length];

        long test =  reader.GetBytes(3, 0, buffer, 0, buffer.Length);

        stream.Write(buffer, 0, (int)test);

        stream.Flush();
        stream.Close();


        Image1.ImageUrl = "~/tmp/" + fileName;
       
    }
}

#4


补充,数据库表的结构为
ImageID 自动增长列
ImageContent image --二进制字段
ImageLength --图片长度

#5


引用 4 楼 wsmall1 的回复:
补充,数据库表的结构为 
ImageID 自动增长列 
ImageContent image --二进制字段 
ImageLength --图片长度


不好意思,没有 ImageID 自动增长列  这个字段
应该为
ImageContent image --二进制字段 
ImageLength --图片长度

#6


up

#7


SqlDataReader myreader = objcmd.ExecuteReader();
            if (myreader.Read())
            {
                   里面如何写,请大家解决一下。
                      要求:先要读出(myread)[zp]字段,然后转化成string类型,给一个string字符串赋值。还有在picturebox接收图片显示。
                      代码,写一下,谢谢各位大虾。
            }