可以将byte[]转换成流来进行读写。
我写了一个将byte[]数组读取,并存为一个Image对象的方法
//此行是对LoadImage()方法的调用
this.pictureBox.Image = this.LoadImage(dataRow["Pic"] as byte[]);
======================================================================
/// <summary>
/// 读取图片
/// </summary>
/// <param name="picData">byte[]数组</param>
/// <returns></returns>
public Image LoadImage(byte[] picData)
{
// make sure this is an embedded object
if (picData == null) return null;
// load the picture
Image img = null;
try
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(picData);
img = Image.FromStream(ms);
}
catch{}
// return what we got
return img;
}

解决方案 »

  1.   

    hu77wei(木村) ,
                  然后把这个img,用sql语句insert或update进数据库就行了,是吗?
                  那怎样从数据库读取出来然后转化成byte数组呢?
      

  2.   

    317043 HOW TO: Read and Write a File to and from a BLOB Column by Using C#.Net
    http://support.microsoft.com/?id=317043317034 HOW TO: Read and Write a File to and from a BLOB Column by Using VB.Net
    http://support.microsoft.com/?id=317034317016 HOW TO: Read and Write a File to/from a BLOB Column using ADO.NET and C#
    http://support.microsoft.com/?id=317016316887 HOW TO: Read and Write a File to and from a BLOB Column by Using ADO.NET and VB
    http://support.microsoft.com/?id=316887
      

  3.   

    要想把这个img写入数据库必须要把他转换成byte[]数组。
    ===============================================//这行语句就是将数据库读取出数据转化成byte数组
    //这个dataRow是个DataTable中的一行纪录,"Pic"是字段名
    //dataRow["Pic"]就是取出该行该列的数据
    dataRow["Pic"] as byte[];
    ===================================================
      

  4.   

    思归大哥,谢谢啊,可是我是初学有些看不懂,现在时间特紧,来不及细看了,以后一定
               好好学习! 
    hu77wei(木村),已经是byte数组了,怎末写入数据库呀?
                   读取数据库时也不对:(
      

  5.   

    re:然后把这个img,用sql语句insert或update进数据库就行了,是吗?
    ================================================================
    我通常是用SqlDataAdapter将数据库中的表Fill到一个DataSet中,然后
    再取出DataSet中的DataTable,然后对此DataTable中的数据进行处理。
    example:
      dataTable.Rows[0][0]可以返回或获取一个object对象,任何类型都可以隐式转换成object对象,
    自然就可以将byte[]存入到你的DataTable中,然后sqlDataAdapter.Update(dataSet);
    将改变的数据提交到数据库中了。
      

  6.   

    感觉用单纯的Sql语句来处理比较麻烦,所以我通常用数据集的方式来解决。
      

  7.   

    hu77wei(木村) 大哥:
    对呀我也是将取出的数据放到DataTable中
    可是 我想把image型的数据取出来咋取呀?
    string str=dataTable.Rows[0][“image”].tostring();//列image,就是image型的
    我这样时出错;
    即使这样可以了,那怎样放到一个byte型的数组里呢?
    还有阿,我也是想先把byte[] 存入到DataTable里再更新数据库
    可就是不知道咋存才来问的
    木村哥把你的那部分源码给我瞧瞧好不?
      

  8.   

    我是想先把image型的取出放到string 中再改成byte[]类型
    要不object型的咋变成其他类型呀?
    按照大哥你的 as byte[];

    byte[] aa=new byte[2];
    aa=ds.table["tab"].rows[0]["Content"] as byte[];
    然后我Console.Writeline(aa);
    打印出来就是“System.byte[]”,和我tostring(),再赋给byte[]结果一样
    这是咋回事呀?
      

  9.   

    string str=dataTable.Rows[0][“image”].tostring();//列image,就是image型的
    image类型的数据应该不能用tostring的,这个函数只是针对字符数据的。二进制数据的存取,msdn上面有例子,自己查一下吧
      

  10.   

    首先,你有两个理解上的错误。
    第一,数据库中的image类型不能等同于.net中的image类型的对象。数据库中的image类型是用二进制代码的形式存放的,而且长度和所存储的图片文件的大小有关,当转换成byte[]后的大小也是不确定的。
    第二,dataTable.Rows[0][“image”].tostring();所得到的结果是一个类型名。根本都不可能得到你所想要的数据,因为.net重载了byte[]类型的ToString().
    =========================================================================
    你的代码:
    byte[] aa=new byte[2];
    aa=ds.table["tab"].rows[0]["Content"] as byte[];
    改为:
    byte[] aa;
    aa=ds.table["tab"].rows[0]["Content"] as byte[];
    if(aa==null)return;
    //此时的aa就存入了数据库中的image.
      

  11.   

    ds.table["tab"].rows[0]["Content"]= aa;
    然后sqlAdapter.Update(ds);