新手!在C#中如何将图片存到SQL中~!~

h1726170 2007-05-02 07:26:56
如何将图片存入SQL中在线等待~!~!希望能把关键代码发上来谢谢~!
...全文
516 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaotupansy 2007-05-10
  • 打赏
  • 举报
回复
顺道问一下,怎么显示图片啊
把他从数据库里面读取出来的时候能直接显示?
BlackPointofSun 2007-05-06
  • 打赏
  • 举报
回复
两害相权择其轻
jackson1987 2007-05-06
  • 打赏
  • 举报
回复
我认可你(BlackPointofSun)的说法
但是那也是有缺点的阿
BlackPointofSun 2007-05-05
  • 打赏
  • 举报
回复
最好还是存路径,数据库。。。每个字节都是钱啊~~
chaochao6078 2007-05-05
  • 打赏
  • 举报
回复
如果不想将图片转化为二进制,就把图片的绝对路径存入数据库
shxmh 2007-05-05
  • 打赏
  • 举报
回复
1.先将图片转换成Stream
2.将Stream转换成byte[]数组
3.再将byte[]数组附给INSERT或Updata语句的SQLCommand或OLEDBCommand的参数
4.执行SQLCommand的ExecuteNonQuery()方法
xiaoliangwh 2007-05-05
  • 打赏
  • 举报
回复
帮顶
jeffcn2 2007-05-03
  • 打赏
  • 举报
回复
我这个比较少。。。也没经过什么处理,是用vb.net做的。不知道能不能帮上忙
Dim file As New FileInfo("图片路径")
Dim stream1 As Stream = file.OpenRead
Dim imagelen As Integer = file.Length
Dim imagedata(imagelen) As Byte

stream1.Read(imagedata, 0, imagelen)

Try

ds.Tables(0).Rows(0)(2) = imagedata
Dim sqlcom As New SqlClient.SqlCommandBuilder(sqlad)
sqlad.UpdateCommand = sqlcom.GetUpdateCommand
sqlad.Update(ds.Tables(0))
MessageBox.Show("成功")
Catch ex As IOException
MessageBox.Show("失败")
End Try

cinray 2007-05-02
  • 打赏
  • 举报
回复
Mark2
wqxhome 2007-05-02
  • 打赏
  • 举报
回复
原理是在数据库建立一个保存二进制数据的字段。在存入数据库时将图片的数据读入为二进制数据的字节数组,然后按照一般的方法将数据存入数据库
h1726170 2007-05-02
  • 打赏
  • 举报
回复
还是没有弄明白~!~!郁闷~!
yan53125 2007-05-02
  • 打赏
  • 举报
回复
mark
bingchener 2007-05-02
  • 打赏
  • 举报
回复
同意amandag(高歌)
amandag 2007-05-02
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;

class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add("Test","1.jpg");
}

public static void Add(string categoryName, string filePath)
{
// byte [] photo = GetPhoto(filePath);

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

byte [] photo = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=Northwind");
SqlCommand cmd = new SqlCommand("INSERT INTO Categories(CategoryName, Picture) VALUES (@CategoryName, @Picture)", cn);

cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15).Value = categoryName;
cmd.Parameters.Add("@Picture", SqlDbType.Image, photo.Length).Value = photo;

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}

public static byte [] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

byte [] photo = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

return photo;
}
}

110,538

社区成员

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

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

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