cashcho(翻译)
关键字 SQL Server images
出处 http://www.bipinjoshi.com/
介绍
有时候我们需要保存一些binary data进数据库。SQL Server提供一个叫做image的特殊数据类型供我们保存binary data。Binary data可以是图片、文档等。在这篇文章中我们将看到如何在SQL Server中保存和输出图片。
建表
为了试验这个例子你需要一个含有数据的table(你可以在现在的库中创建它,也可以创建一个新的数据库),下面是它的结构:
| Column Name | Datatype | Purpose |
| ID | Integer | identity column Primary key |
| IMGTITLE | Varchar(50) | Stores some user friendly title to identity the image |
| IMGTYPE | Varchar(50) | Stores image content type. This will be same as recognized content types of ASP.NET |
IMGDATA |
Image | Stores actual image or binary data. |
保存images进SQL Server数据库
为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件。确信你设定了Form的encType属性为multipart/form-data。
|
Stream imgdatastream = File1.PostedFile.InputStream;www.c hinaitpower.comlOPhU7 int imgdatalen = File1.PostedFile.ContentLength;www.c hinaitpower.comlOPhU7 string imgtype = File1.PostedFile.ContentType;www.c hinaitpower.comlOPhU7 string imgtitle = TextBox1.Text;www.c hinaitpower.comlOPhU7 byte[] imgdata = new byte[imgdatalen];www.c hinaitpower.comlOPhU7 int n = imgdatastream.Read(imgdata,0,imgdatalen);www.c hinaitpower.comlOPhU7 string connstr=www.c hinaitpower.comlOPhU7 ((NameValueCollection)Context.GetConfigwww.c hinaitpower.comlOPhU7 ("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7 SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7 SqlCommand command = new SqlCommandwww.c hinaitpower.comlOPhU7 ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)www.c hinaitpower.comlOPhU7 VALUES ( @imgtitle, @imgtype,@imgdata )", connection );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramTitle = new SqlParameterwww.c hinaitpower.comlOPhU7 ("@imgtitle", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7 paramTitle.Value = imgtitle;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramTitle);www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramData = new SqlParameterwww.c hinaitpower.comlOPhU7 ( "@imgdata", SqlDbType.Image );www.c hinaitpower.comlOPhU7 paramData.Value = imgdata;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramData );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramType = new SqlParameterwww.c hinaitpower.comlOPhU7 ( "@imgtype", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7 paramType.Value = imgtype;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramType );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 connection.Open();www.c hinaitpower.comlOPhU7 int numRowsAffected = command.ExecuteNonQuery();www.c hinaitpower.comlOPhU7 connection.Close();www.c hinaitpower.comlOPhU7 |
从数据库中输出图片
现在让我们从数据库中取出我们刚刚保存的图片,在这儿,我们将直接将图片输出至浏览器。你也可以将它保存为一个文件或做任何你想做的。
|
private void Page_Load(object sender, System.EventArgs e)www.c hinaitpower.comlOPhU7 {www.c hinaitpower.comlOPhU7 string imgid =Request.QueryString["imgid"];www.c hinaitpower.comlOPhU7 string connstr=((NameValueCollection)www.c hinaitpower.comlOPhU7 Context.GetConfig("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7 string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "www.c hinaitpower.comlOPhU7 + imgid;www.c hinaitpower.comlOPhU7 SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7 SqlCommand command = new SqlCommand(sql, connection);www.c hinaitpower.comlOPhU7 connection.Open();www.c hinaitpower.comlOPhU7 SqlDataReader dr = command.ExecuteReader();www.c hinaitpower.comlOPhU7 if(dr.Read())www.c hinaitpower.comlOPhU7 {www.c hinaitpower.comlOPhU7 Response.ContentType = dr["imgtype"].ToString();www.c hinaitpower.comlOPhU7 Response.BinaryWrite( (byte[]) dr["imgdata"] );www.c hinaitpower.comlOPhU7 }www.c hinaitpower.comlOPhU7 connection.Close();www.c hinaitpower.comlOPhU7 }www.c hinaitpower.comlOPhU7 |
在上面的代码中我们使用了一个已经打开的数据库,通过datareader选择images。接着用Response.BinaryWrite代替Response.Write来显示image文件。
希望您喜欢这些文章,如有任何意见和建议请致信webmaster@bipinjoshi.com。
本文转载自中国软件(http://www.csdn.net)。

帮了我很大的忙
BK网络学院主要内容:平面设计教程,网站开发在线教程,网页制作教程,服务器教程,网络编程,数据库教程等。