可以储存照片的字段类型是_sql server 中 哪个字段类型可以储存图象?
展開全部
可以將圖片上傳到指定目錄并將路徑記錄在數據庫中,要用的時候再從數據庫中取路徑根據路徑找到圖片。e68a84e8a2ad62616964757a686964616f31333234303663
也可以直接存在數據庫中。SqlServer中用Image列來保存圖片
兩者各有千秋,從性能上考慮應用第一種,從安全上考慮應用第二種
以下為存在數據庫中的例子:來源于百度
首先在SQL Server中建立一個圖片存儲的數庫表,ImageData Column為圖象二進制數據儲存字段,ImageContentType Column為圖象文件類型記錄字段,ImageDescription Column為儲蓄圖象文件說明字段,ImageSize Column為儲存圖象文件長度字段,結構如下:
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程序內容如下:
上傳圖片| 上傳圖片(選擇你要上傳的圖片) | |
文件說明(添加上傳圖片說明,如:作者、出處) | |
//-------------------------------------------------------------------
//UpLoadImage.cs程序內容如下:
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數組
Stream StreamObject = UpFile.InputStream; //建立數據流對像
//讀取圖象文件數據,FileByteArray為數據儲存體,0為數據指針位置、FileLnegth為數據長度
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; //記錄文件類型
//把其它單表數據記錄上傳
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!你已經成功上傳你的圖片";//提示上傳成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
//----------------------------------------------------------------------
//好了,圖片已經上傳到數據庫,現在還要干什么呢?當然是在數據庫中讀取及顯示在Web頁中啦,請看以下程序:
//ReadImage.aspx程序內容如下:
/-----------------------------------------------------------------------
//----------------------------------------------------------------------
//ReadImage.cs程序內容如下:
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
//建立數據庫鏈接
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"];//設定輸出文件類型
//輸出圖象文件二進制數制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
//很簡單吧^_^
}
}
}
//--------------------------------------------------------------------
//最后,我們當然要把它在Web頁面顯示出來啦
//ShowImage.hml
這個是從數據庫讀取出來的圖象:
//------------------------------------------------------------------
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
總結
以上是生活随笔為你收集整理的可以储存照片的字段类型是_sql server 中 哪个字段类型可以储存图象?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序弹出层禁止列表滑动_是时候展现真正
- 下一篇: vue 如何生成一个dom元素_通过一个