如何从SQL数据库中读取二进制数据

sardineany 2010-06-25 01:59:46
二进制数据已经存进去了(列名为A)。我想把他存到一个byte数组中。。要怎么实现。。
...全文
889 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yongheart1321 2011-11-04
  • 打赏
  • 举报
回复
有源码吗?
sardineany 2010-06-25
  • 打赏
  • 举报
回复
要的就是他。。谢谢了。。结贴给分
zqf861126 2010-06-25
  • 打赏
  • 举报
回复
你什么换一下就可以了啊 ,,主要是思路 啊
lostuser 2010-06-25
  • 打赏
  • 举报
回复
类型转换

(byte[])xxx
sardineany 2010-06-25
  • 打赏
  • 举报
回复
是winform。。。
zqf861126 2010-06-25
  • 打赏
  • 举报
回复
你试试
读出并生成图片到物理位置
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];//读取第一个图片的位流
int ArraySize= MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限

FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //-- 写入到c:\00.jpg。
conn.Close();
Console.WriteLine("读取成功");//查看硬盘上的文件
}
}
-深白色- 2010-06-25
  • 打赏
  • 举报
回复
直接读取赋值给byte[]不就行了么?二进制数据在数据库不是byte类型的?
Zhanlixin 2010-06-25
  • 打赏
  • 举报
回复
DataSet dsResuls = null;
string strSQL = @"SELECT A from table";
DataAccessTool.ExecuteDataSet(strSQL, out dsResuls);
byte[] fileByte fileByte = (byte[])dsResuls.Tables[0].Rows[0][0];
宝_爸 2010-06-25
  • 打赏
  • 举报
回复
参考下列代码


private void Page_Load(object sender, System.EventArgs e)
{
//get the image id from the url
string ImageId = Request.QueryString["img"];

//build our query statement
string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;

SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["DSN"].ToString() );
SqlCommand command = new SqlCommand( sqlText, connection);

//open the database and get a datareader
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if ( dr.Read()) //yup we found our image
{
Response.ContentType = dr["img_contenttype"].ToString();
Response.BinaryWrite( (byte[]) dr["img_data"] );
}
connection.Close();

}
}
sardineany 2010-06-25
  • 打赏
  • 举报
回复
DDD

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧