日期:2014-05-17  浏览次数:20575 次

请问如何将二进制流从数据库里读取出来啊?
我是这样保存的
byte[] fileByte = null;
fileByte = new byte[files[0].ContentLength];


数据库里面是Image类型,

现在我要读取出来
public byte [] WJL
{
  get { return _wjl; }
  set { _wjl = value; }


//WJL = Convert.ToByte( dr["WJL"].ToString()); 这里应该怎么写啊?

------解决方案--------------------
http://www.2cto.com/kf/201201/117016.html
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e) 
{
string id = Request.Params["Id"];

DataSet ds = service.getimage(id);
if (ds.Tables[0].Rows.Count == 0)
return;
byte[] bydata = (byte[])ds.Tables[0].Rows[0][0];

Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(bydata);
}
------解决方案--------------------
C# code
/// <summary>
        /// 从数据库读取相应图片
        /// </summary>
        /// <param name="name">图片名称</param>
        /// <returns>返回该图片</returns>
        public static Image GetImage(string name)
        {
            SqlConnection con = new SqlConnection(SqlConString);
            SqlCommand cmd = con.CreateCommand();
            SqlParameter pname = new SqlParameter("@name", name);
            cmd.Parameters.Add(pname);
            cmd.CommandText = "select image from MyTable where name=@name";
            con.Open();
            byte[] MyData = (byte[])cmd.ExecuteScalar();
            MemoryStream ms = new MemoryStream(MyData, 0, MyData.Length);
            ms.Seek(0, SeekOrigin.Begin);
            Image image = Image.FromStream(ms);
            con.Close();
            return image;
        }

------解决方案--------------------
files[0].ContentLength这个是内容吧。
byte[] data=system.io.file.read();
------解决方案--------------------
探讨

我想说的是,我这样写上传对还是不对,不对该怎么写啊?

byte[] fileByte = null;
fileByte = new byte[files[0].ContentLength];