日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可以储存照片的字段类型是_sql server 中 哪个字段类型可以储存图象?

發(fā)布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可以储存照片的字段类型是_sql server 中 哪个字段类型可以储存图象? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

展開全部

可以將圖片上傳到指定目錄并將路徑記錄在數(shù)據(jù)庫中,要用的時候再從數(shù)據(jù)庫中取路徑根據(jù)路徑找到圖片。e68a84e8a2ad62616964757a686964616f31333234303663

也可以直接存在數(shù)據(jù)庫中。SqlServer中用Image列來保存圖片

兩者各有千秋,從性能上考慮應(yīng)用第一種,從安全上考慮應(yīng)用第二種

以下為存在數(shù)據(jù)庫中的例子:來源于百度

首先在SQL Server中建立一個圖片存儲的數(shù)庫表,ImageData Column為圖象二進(jìn)制數(shù)據(jù)儲存字段,ImageContentType Column為圖象文件類型記錄字段,ImageDescription Column為儲蓄圖象文件說明字段,ImageSize Column為儲存圖象文件長度字段,結(jié)構(gòu)如下:

CREATE TABLE [dbo].[ImageStore] (

[ImageID] [int] IDENTITY (1, 1) NOT NULL ,

[ImageData] [image] NULL ,

[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,

[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,

[ImageSize] [int] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

*/

//UpLoadImage.aspx程序內(nèi)容如下:

上傳圖片
上傳圖片(選擇你要上傳的圖片)

文件說明(添加上傳圖片說明,如:作者、出處)

//-------------------------------------------------------------------

//UpLoadImage.cs程序內(nèi)容如下:

using System;

using System.Web;

using System.IO;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace UploadImage

{

public class UploadImage : Page {

protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控件對象

protected TextBox txtDescription;

protected Label txtMessage;

protected Int32 FileLength = 0; //記錄文件長度變量

protected void Button_Submit(System.Object sender, System.EventArgs e) {

HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile對象,用于讀取圖象文件屬性

FileLength = UpFile.ContentLength; //記錄文件長度

try {

if (FileLength == 0) { //文件長度為零時

txtMessage.Text = "請你選擇你要上傳的文件";

} else {

Byte[] FileByteArray = new Byte[FileLength]; //圖象文件臨時儲存Byte數(shù)組

Stream StreamObject = UpFile.InputStream; //建立數(shù)據(jù)流對像

//讀取圖象文件數(shù)據(jù),FileByteArray為數(shù)據(jù)儲存體,0為數(shù)據(jù)指針位置、FileLnegth為數(shù)據(jù)長度

StreamObject.Read(FileByteArray,0,FileLength);

//建立SQL Server鏈接

SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;");

String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";

SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);

CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;

CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = UpFile.ContentType; //記錄文件類型

//把其它單表數(shù)據(jù)記錄上傳

CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text;

//記錄文件長度,讀取時使用

CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength;

Con.Open();

CmdObj.ExecuteNonQuery();

Con.Close();

txtMessage.Text = "

OK!你已經(jīng)成功上傳你的圖片";//提示上傳成功

}

} catch (Exception ex) {

txtMessage.Text = ex.Message.ToString();

}}}}

//----------------------------------------------------------------------

//好了,圖片已經(jīng)上傳到數(shù)據(jù)庫,現(xiàn)在還要干什么呢?當(dāng)然是在數(shù)據(jù)庫中讀取及顯示在Web頁中啦,請看以下程序:

//ReadImage.aspx程序內(nèi)容如下:

/-----------------------------------------------------------------------

//----------------------------------------------------------------------

//ReadImage.cs程序內(nèi)容如下:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace ReadImage {

public class MainDisplay : System.Web.UI.Page {

public void Page_Load(System.Object sender, System.EventArgs e) {

int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID為圖片ID

//建立數(shù)據(jù)庫鏈接

SqlConnection Con = new SqlConnection("Data Source=KING;Initial Catalog=testdb;User ID=sa;Pwd=;");

String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID";

SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);

CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;

Con.Open();

SqlDataReader SqlReader = CmdObj.ExecuteReader();

SqlReader.Read();

Response.ContentType = (string)SqlReader["ImageContentType"];//設(shè)定輸出文件類型

//輸出圖象文件二進(jìn)制數(shù)制

Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);

Response.End();

Con.Close();

//很簡單吧^_^

}

}

}

//--------------------------------------------------------------------

//最后,我們當(dāng)然要把它在Web頁面顯示出來啦

//ShowImage.hml

這個是從數(shù)據(jù)庫讀取出來的圖象:

//------------------------------------------------------------------

已贊過

已踩過<

你對這個回答的評價是?

評論

收起

總結(jié)

以上是生活随笔為你收集整理的可以储存照片的字段类型是_sql server 中 哪个字段类型可以储存图象?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。