注意:是用winForms应用程序做。
今天一天就在解决这个问题,很郁闷,还是没解决,特此上来求救。
8 个解决方案
#1
up
#2
private SqlConnection mycnn=null;
//保存图片到数据库
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
SqlCommand mycmd=new SqlCommand("update picture set picture=@a where ID=1",mycnn);
FileStream mystream=new FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@a",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@a"].Value=box;
//更新
mycmd.ExecuteNonQuery();
MessageBox.Show("ok");
mystream.Close();
mycnn.Close();
//保存图片到数据库
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
SqlCommand mycmd=new SqlCommand("update picture set picture=@a where ID=1",mycnn);
FileStream mystream=new FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@a",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@a"].Value=box;
//更新
mycmd.ExecuteNonQuery();
MessageBox.Show("ok");
mystream.Close();
mycnn.Close();
#3
//从数据库中读出图片
try
{
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
MessageBox.Show("ok.mycnn.open");
SqlCommand mycmd=new SqlCommand("select * from picture",mycnn);
SqlDataReader myrd=mycmd.ExecuteReader();
if(myrd.Read())
{
//读取图片
if(!myrd.IsDBNull(1))
{
byte []box=(byte [])myrd[1];
//构造流
Stream stream1=new MemoryStream(box);
this.pictureBox2.Image=System.Drawing.Image.FromStream(stream1);
stream1.Close();
}
else
{
MessageBox.Show("该字段是NULL");
}
}
mycnn.Close();
}
catch(Exception my)
{
MessageBox.Show(my.Message.ToString());
}
}
#4
数据据字段设为什么类型?image好像只有16长度啊?
我用的
insert into photo values('box')插入数据库行不行?
我测试过到数据库中的长度只有13长度,无法显示图片,怎么回事?
我用的
insert into photo values('box')插入数据库行不行?
我测试过到数据库中的长度只有13长度,无法显示图片,怎么回事?
#5
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
#6
字段为image类型,我没有问题呀
#7
保存
通过:pictureboxy读,sql="";//保存SQL
SqlCommand cmd=new SqlCommand(sql,connect);
MemoryStream s = new MemoryStream();
picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = s.ToArray();
s.Close();
cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);
cmd.Parameters["@i"].Value=b;
cmd.ExecuteNonQuery();
private void Readphoto(string id)
{
try
{ //读取图象
string ls_sql=“”插入数据库的语句
this.picPhoto.Image=null;
this.picPhoto.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,connetct);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
byte[] b = (byte[])reader[0];
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
picPhoto.Image = image;
s.Close();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过:pictureboxy读,sql="";//保存SQL
SqlCommand cmd=new SqlCommand(sql,connect);
MemoryStream s = new MemoryStream();
picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = s.ToArray();
s.Close();
cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);
cmd.Parameters["@i"].Value=b;
cmd.ExecuteNonQuery();
private void Readphoto(string id)
{
try
{ //读取图象
string ls_sql=“”插入数据库的语句
this.picPhoto.Image=null;
this.picPhoto.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,connetct);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
byte[] b = (byte[])reader[0];
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
picPhoto.Image = image;
s.Close();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#8
转为byte类型后存入Image字段。
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
#1
up
#2
private SqlConnection mycnn=null;
//保存图片到数据库
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
SqlCommand mycmd=new SqlCommand("update picture set picture=@a where ID=1",mycnn);
FileStream mystream=new FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@a",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@a"].Value=box;
//更新
mycmd.ExecuteNonQuery();
MessageBox.Show("ok");
mystream.Close();
mycnn.Close();
//保存图片到数据库
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
SqlCommand mycmd=new SqlCommand("update picture set picture=@a where ID=1",mycnn);
FileStream mystream=new FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@a",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@a"].Value=box;
//更新
mycmd.ExecuteNonQuery();
MessageBox.Show("ok");
mystream.Close();
mycnn.Close();
#3
//从数据库中读出图片
try
{
mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
mycnn.Open();
MessageBox.Show("ok.mycnn.open");
SqlCommand mycmd=new SqlCommand("select * from picture",mycnn);
SqlDataReader myrd=mycmd.ExecuteReader();
if(myrd.Read())
{
//读取图片
if(!myrd.IsDBNull(1))
{
byte []box=(byte [])myrd[1];
//构造流
Stream stream1=new MemoryStream(box);
this.pictureBox2.Image=System.Drawing.Image.FromStream(stream1);
stream1.Close();
}
else
{
MessageBox.Show("该字段是NULL");
}
}
mycnn.Close();
}
catch(Exception my)
{
MessageBox.Show(my.Message.ToString());
}
}
#4
数据据字段设为什么类型?image好像只有16长度啊?
我用的
insert into photo values('box')插入数据库行不行?
我测试过到数据库中的长度只有13长度,无法显示图片,怎么回事?
我用的
insert into photo values('box')插入数据库行不行?
我测试过到数据库中的长度只有13长度,无法显示图片,怎么回事?
#5
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
#6
字段为image类型,我没有问题呀
#7
保存
通过:pictureboxy读,sql="";//保存SQL
SqlCommand cmd=new SqlCommand(sql,connect);
MemoryStream s = new MemoryStream();
picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = s.ToArray();
s.Close();
cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);
cmd.Parameters["@i"].Value=b;
cmd.ExecuteNonQuery();
private void Readphoto(string id)
{
try
{ //读取图象
string ls_sql=“”插入数据库的语句
this.picPhoto.Image=null;
this.picPhoto.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,connetct);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
byte[] b = (byte[])reader[0];
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
picPhoto.Image = image;
s.Close();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过:pictureboxy读,sql="";//保存SQL
SqlCommand cmd=new SqlCommand(sql,connect);
MemoryStream s = new MemoryStream();
picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = s.ToArray();
s.Close();
cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);
cmd.Parameters["@i"].Value=b;
cmd.ExecuteNonQuery();
private void Readphoto(string id)
{
try
{ //读取图象
string ls_sql=“”插入数据库的语句
this.picPhoto.Image=null;
this.picPhoto.Refresh();
SqlCommand cmd=new SqlCommand(ls_sql,connetct);
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read())
{
byte[] b = (byte[])reader[0];
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
picPhoto.Image = image;
s.Close();
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#8
转为byte类型后存入Image字段。
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635