如何将图片存到数据库blob字段

yuduo1112 2012-02-08 04:13:36
完全没头绪啊,从没弄过这什么blob字段
...全文
347 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuduo1112 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bbb332 的回复:]
C# code
//将图像保存到SQL server2000的Image字段中

private void button2_Click_1(object sender, System.EventArgs e)

{

string pathName;

if (this.openFileDialog1.ShowDialog()==System.Windows.……
[/Quote]

大哥,我的是web中的image啊
yuduo1112 2012-02-10
  • 打赏
  • 举报
回复
大虾们在哪里啊
yuduo1112 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 flyerwing 的回复:]
存二进制就可以的和IMAGE好象基本一样
[/Quote]

查不到如何把b/s上面的system.web.ui.webcontrols.image转化为二进制,都是system.drawing.image的转化
flyerwing 2012-02-10
  • 打赏
  • 举报
回复
存二进制就可以的和IMAGE好象基本一样
yuduo1112 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
自己不会搜啊
http://archive.cnblogs.com/a/1914480/
[/Quote]

还是不行,我的是b/s系统,不知道如何将页面上的image中的图片转为二进制
bbb332 2012-02-10
  • 打赏
  • 举报
回复
//将图像保存到SQL server2000的Image字段中

private void button2_Click_1(object sender, System.EventArgs e)

{

string pathName;

if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)

{

pathName = this.openFileDialog1.FileName;

System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);

this.pictureBox1.Image = img;



//将图像读入到字节数组

System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

byte[] buffByte = new byte[fs.Length];

fs.Read(buffByte,0,(int)fs.Length);

fs.Close();

fs = null;



//建立Command命令

string comm = @"Insert into table1(img,name) values(@img,@name)";

this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();

this.sqlCommand1.CommandType = System.Data.CommandType.Text ;

this.sqlCommand1.CommandText = comm;

this.sqlCommand1.Connection = this.sqlConnection1 ;

//创建Parameter

this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);

this.sqlCommand1.Parameters[0].Value = buffByte;

this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

this.sqlCommand1.Parameters[1].Value =pathName.Substring(pathName.LastIndexOf("http://www.cnblogs.com/moss_tan_jun/admin/file://")+1/);

try

{

this.sqlConnection1.Open();

this.sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

}

catch(System.Exception ee)

{

MessageBox.Show(ee.Message );

}

buffByte = null;

this.FillListBox();

}

读取:

从数据库读图片到picturebox

SqlConnection conn=new SqlConnection(@"data source=chenyuming2004VSdotNET;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;

yuduo1112 2012-02-10
  • 打赏
  • 举报
回复
单机了 顶一下
yuduo1112 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
自己不会搜啊
http://archive.cnblogs.com/a/1914480/
[/Quote]

汗,这个解决了,现在把blob取出来变为图片放到控件上又难住了..
熙风 2012-02-08
  • 打赏
  • 举报
回复
存二进制
bdmh 2012-02-08
  • 打赏
  • 举报
回复

110,538

社区成员

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

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

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