请问C#怎么用二进制流存储图片到Sql Server中.?

如题:能否说下其中的步骤,后者参考资料..谁有详细的代码就好了.!... 如题:

能否说下其中的步骤, 后者参考资料..

谁有详细的代码就好了.!
展开
 我来答
乌龟追宝马
推荐于2016-08-09 · TA获得超过100个赞
知道小有建树答主
回答量:154
采纳率:0%
帮助的人:105万
展开全部
正好我手里有这样的源码请你参考一下:
首先..定义一个函数..将图片转化为二进制码
//定义将图片转化为长二进制代码的函数getphoto()
public Byte[] getphoto(string photopath)
{
string str = photopath;
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[file.Length];
file.Read(bytBLOBData, 0, bytBLOBData.Length);
file.Close();
return bytBLOBData;
}//这是定义函数..

然后..就是将转换成二进制码的图片插入数据库中..下面是简单的也是重要的sql语句..
if (this.pictureBox1.Image != null)
{
sql1 = sql1 + ",Photo";
sql2 = sql2 + ",bytBLOBData";
Byte[] bytBLOBData = getphoto(openFileDialog1.FileName);
cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData));
}

接下来..是读取...

string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString();
OleDbCommand cmd = new OleDbCommand(sql, connection1);
if (Convert.DBNull != cmd.ExecuteScalar())
pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()));//读取长二进制为图片..
chenxfsoft
2009-01-04 · TA获得超过1380个赞
知道大有可为答主
回答量:2418
采纳率:0%
帮助的人:1823万
展开全部
给你的我VB.net的代码,供你参考,C#不会,但我想你能看懂.

#Region "二进制文件的存储函数"

Public Function BinaryToFile(ByRef TableRowColItem As Object, ByVal FileName As String) As Boolean

Dim data As Byte() = TableRowColItem
Dim myfilestream As New System.IO.FileStream(FileName, IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
Return True
End Function

Public Function BinaryToImage(ByRef TableRowColItem As Object, ByRef image As Image) As Boolean

Dim data As Byte() = TableRowColItem

Dim imgStream As New System.IO.MemoryStream '(data)
imgStream.Write(data, 0, data.Length)
image = System.Drawing.Image.FromStream(imgStream)
imgStream.Close()
imgStream.Dispose()

Return True
End Function

'ok
Public Function BinaryFromFile(ByVal FileName As String, ByRef TableRowColItem As Object) As Boolean

Using myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
TableRowColItem = data

End Using

Return True
End Function

Public Function BinaryFromImage(ByRef Image As Image, ByRef TableRowColItem As Object) As Boolean

Dim imgStream As New MemoryStream
Dim b As New Bitmap(Image)

b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg)

Dim data As Byte() = imgStream.GetBuffer
TableRowColItem = data
data = TableRowColItem
imgStream.Close()
imgStream.Dispose()

Return True
End Function

Public Function BinaryFromImage(ByVal Image As Image, ByRef TableRowColItem As Object, ByVal imgFormat As System.Drawing.Imaging.ImageFormat) As Boolean
Dim imgStream As New MemoryStream

Dim data As Byte() = imgStream.GetBuffer

TableRowColItem = data
data = TableRowColItem
imgStream.Close()
Return True
End Function

#End Region
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
靳璞频清润
2019-09-05 · TA获得超过3985个赞
知道大有可为答主
回答量:3144
采纳率:31%
帮助的人:211万
展开全部
//定义将图片转化为长二进制代码的函数getphoto()
public
Byte[]
getphoto(string
photopath){
string
str
=
photopath;
FileStream
file
=
new
FileStream(str,
FileMode.Open,
FileAccess.Read);
Byte[]
bytBLOBData
=
new
Byte[file.Length];
file.Read(bytBLOBData,
0,
bytBLOBData.Length);
file.Close();
return
bytBLOBData;
if
(this.pictureBox1.Image
!=
null){
sql1
=
sql1
+
",Photo";
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友94f02eb
2009-01-04 · TA获得超过8612个赞
知道大有可为答主
回答量:7955
采纳率:74%
帮助的人:4422万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
程序员小p
2009-01-05 · TA获得超过145个赞
知道答主
回答量:49
采纳率:0%
帮助的人:36.1万
展开全部
顶下。关注这个问题。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式