高分求助,怎么在winform里将图片保存到sqlserver

kwklover 2004-05-07 01:33:04
我的思路是这样的:
1,获得图片路径
2,将图片转换成byte[]插入数据库

但怎么获得图片路径呢?请各位大侠赐教了!
如果有完整代码,那就更好了

thank you very much!
...全文
73 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
BearRui 2004-05-07
  • 打赏
  • 举报
回复
SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
从数据库读图片到picturebox
BearRui 2004-05-07
  • 打赏
  • 举报
回复
SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();
把图片读到pictureBox,再写入数据库
greatsft 2004-05-07
  • 打赏
  • 举报
回复
读取图象:
try
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.picPhoto.Image= Image.FromStream(stmBLOBData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
songbird 2004-05-07
  • 打赏
  • 举报
回复
试下绑定图像数据列
kwklover 2004-05-07
  • 打赏
  • 举报
回复
to:greatsft(C的使者)

插入的图片又怎么读取并显示在picturebox中呢?
wangsaokui 2004-05-07
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3K
greatsft 2004-05-07
  • 打赏
  • 举报
回复
try
{
MemoryStream ms = new MemoryStream ();
picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
m_DataRow[MyTools.g_PhotoField] = myData;

}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}

wish u good luck
Greatsft

111,097

社区成员

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

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

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