可以将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;
}
我写了一个将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;
}
然后把这个img,用sql语句insert或update进数据库就行了,是吗?
那怎样从数据库读取出来然后转化成byte数组呢?
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
===============================================//这行语句就是将数据库读取出数据转化成byte数组
//这个dataRow是个DataTable中的一行纪录,"Pic"是字段名
//dataRow["Pic"]就是取出该行该列的数据
dataRow["Pic"] as byte[];
===================================================
好好学习!
hu77wei(木村),已经是byte数组了,怎末写入数据库呀?
读取数据库时也不对:(
================================================================
我通常是用SqlDataAdapter将数据库中的表Fill到一个DataSet中,然后
再取出DataSet中的DataTable,然后对此DataTable中的数据进行处理。
example:
dataTable.Rows[0][0]可以返回或获取一个object对象,任何类型都可以隐式转换成object对象,
自然就可以将byte[]存入到你的DataTable中,然后sqlDataAdapter.Update(dataSet);
将改变的数据提交到数据库中了。
对呀我也是将取出的数据放到DataTable中
可是 我想把image型的数据取出来咋取呀?
string str=dataTable.Rows[0][“image”].tostring();//列image,就是image型的
我这样时出错;
即使这样可以了,那怎样放到一个byte型的数组里呢?
还有阿,我也是想先把byte[] 存入到DataTable里再更新数据库
可就是不知道咋存才来问的
木村哥把你的那部分源码给我瞧瞧好不?
要不object型的咋变成其他类型呀?
按照大哥你的 as byte[];
我
byte[] aa=new byte[2];
aa=ds.table["tab"].rows[0]["Content"] as byte[];
然后我Console.Writeline(aa);
打印出来就是“System.byte[]”,和我tostring(),再赋给byte[]结果一样
这是咋回事呀?
image类型的数据应该不能用tostring的,这个函数只是针对字符数据的。二进制数据的存取,msdn上面有例子,自己查一下吧
第一,数据库中的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.
然后sqlAdapter.Update(ds);