如何将图片以二进制保存在SQL中

zhongwei002 2010-04-19 08:23:43
我想问问如何将图片以二进制保存在SQL数据库中,因为是新手,所以最好可以给个例子,代码。谢谢!
...全文
842 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongwei002 2010-04-19
  • 打赏
  • 举报
回复
谢谢了
wuyq11 2010-04-19
  • 打赏
  • 举报
回复
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));


SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";

sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

ms.Close();
}
}

SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;


FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@image"].Value=box;
accp_cq 2010-04-19
  • 打赏
  • 举报
回复
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);
sql语句:
string strSql="Insert into test(FImage) values(@FImage)";
SqlCommand cmd=new SqlCommand(strSql,conn);
cmd.Parameters.Add("@FImage",SqlDbType.Image);
cmd.Parameters["@FImage"].Value=PhotoArray;





在WinForm中,使C#语言向SQL Server(2005)存储图片并且将图片从数据库中读取出来,显示在窗体中
步骤:
1.首先,在SQL Server中创建一个表,用来存储图片;
示例代码:
use tempdb
go
if exists (select * from sysobjects where name = 'Images')
drop table Images
go
create table Images
(
BLODID int identity not null,
BLOBData image not null
)

2.打开Visual Studio2005,创建一个WinForm应用程序。向Form1中添加一个PictureBox控件,再添加两个

Button控件,将Button1的Text属性分别设为"保存图片","显示图片";

3.在Form1的代码之中,首先引入命名空间:
using System.Data.SqlCilent;
using System.IO;
using System.Drawing.Imaging;

4.编写"保存图片"按钮的单击事件,用于保存图片;
示例代码:
private void button1_Click(object sender, EventArgs e)
{

try
{
string strConn = "Server=.;DataBase=tempdb;Integrated Security=true";
SqlConnection connection = new SqlConnection(strConn);
string sql = "insert into blobtest (blobdata) values (@blobdata)";
SqlCommand command = new SqlCommand(sql, connection);
//图片路径
string picturePath = @"D:\pic.jpg"; //注意,这里需要指定保存图片的绝对路径和图片

//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
Byte[] mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte,0,mybyte.Length);
fs.Close();
//转换成二进制数据,并保存到数据库
SqlParameter prm = new SqlParameter
("@blobdata",SqlDbType.VarBinary,mybyte.Length,ParameterDirection.Input,false,0,0,null,DataRowVers

ion.Current,mybyte);
command.Parameters.Add(prm);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

5.编写"显示图片"的单击事件,将图片从数据库中读取出来显示在PictureBox之中
示例代码:
private void button2_Click(object sender, EventArgs e)
{
try
{
//创建数据库连接字符串
string strConn = "Server=.;DataBase=tempdb;Integrated Security=True";
//创建SqlConnection对象
SqlConnection connection = new SqlConnection(strConn);
//打开数据库连接
connection.Open();
//创建SQL语句
string sql = "select blodid,blobdata from blobtest order by blodid";
//创建SqlCommand对象
SqlCommand command = new SqlCommand(sql,connection);
//创建DataAdapter对象
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
//创建DataSet对象
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "BLOBTest");
int c = dataSet.Tables["BLOBTest"].Rows.Count;
if (c > 0)
{
Byte[] mybyte = new byte[0];
mybyte = (Byte[])(dataSet.Tables["BLOBTest"].Rows[c-1]["BLOBData"]);
MemoryStream ms = new MemoryStream(mybyte);
pictureBox1.Image = Image.FromStream(ms);
}
connection.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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