




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、如何存儲圖片到 SQL SERVER 數(shù)據(jù)庫中SQL Server 提供了一個特別的數(shù)據(jù)類型: image,它是一個包含 binary 數(shù)據(jù)的類型。下 邊這個例子就向你展示了如何將文本或照片放入到數(shù)據(jù)庫中的辦法。 在這篇文章中我們要看 到如何在 SQL Server 中存儲和讀取圖片。1、建立一個表:在 SQL SERVER 中建立這樣結(jié)構(gòu)的一個表:列名 類型目的ID Integer主鍵 IDIMGTITLEVarchar(50) 圖片的標題IMGTYPEVarchar(50) 圖片類型 . ASP.NET 要以辨認的類型IMGDATAImage 用于存儲二進制數(shù)據(jù)2、存儲圖片到 SQL SE
2、RVER 數(shù)據(jù)庫中為了能存儲到表中,你首先要上傳它們到你的 WEB 服務器上,你可以開發(fā)一個 web form ,它用來將客戶端中 TextBox web control 中的圖片入到你的 WEB 服務器上來。將你的 encType 屬性設置為: myltipart/formdata.Stream imgdatastream = ;int imgdatalen = ;string imgtype = ;string imgtitle = TextBox1.Text;byte imgdata = new byteimgdatalen;int n = imgdatastream.Read(imgd
3、ata,0,imgdatalen);string connstr=(NameValueCollection)Context.GetConfig("appSettings")"connstr"SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( imgtitle, imgtype,imgdata )"
4、;, connection );SqlParameter paramTitle = new SqlParameter("imgtitle", SqlDbType.VarChar,50 );paramTitle.Value = imgtitle;command.Parameters.Add( paramTitle);SqlParameter paramData = new SqlParameter( "imgdata", SqlDbType.Image ); paramData.Value = imgdata;command.Parameters.Add(
5、 paramData );SqlParameter paramType = new SqlParameter( "imgtype", SqlDbType.VarChar,50 ); paramType.Value = imgtype;command.Parameters.Add( paramType );connection.Open();int numRowsAffected = command.ExecuteNonQuery(); connection.Close();3、從數(shù)據(jù)庫中恢復讀取現(xiàn)在讓我們來從 SQL Server 中讀取我們放入的數(shù)據(jù)吧 !我們將要輸出圖片
6、到你的瀏覽器 上,你也可以將它存放到你要的位置。private void Page_Load(object sender, System.EventArgs e) string imgid =Request.QueryString"imgid"string connstr=(NameValueCollection) Context.GetConfig("appSettings")"connstr"string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = &q
7、uot; + imgid; SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand(sql, connection); connection.Open();SqlDataReader dr = command.ExecuteReader(); if(dr.Read()Response.ContentType = dr"imgtype".ToString();Response.BinaryWrite( (byte) dr"imgdata&quo
8、t; ); connection.Close();要注意的是 Response.BinaryWrite 而不是 Response.Write.下面給大家一個用于 C# Winform 的存入、讀取程序。其中不同請大家自己比較!( 為了方便起見,我將數(shù)據(jù)庫字段簡化為二個: imgtitle 和 imgdata。using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.
9、IO;using System.Data.SqlClient;namespace WindowsApplication21/ < summary>/ Form1 的摘要說明。/ < /summary >public class Form1 : System.Windows.Forms.Formprivate System.Windows.Forms.Button button1;/ < summary>/ 必需的設計器變量。/ < /summary >private System.ComponentModel.Container componen
10、ts = null;Catalog=;Dataprivate string ConnectionString = "Integrated Security=SSPI;Initial Source=localhost;"private SqlConnection conn = null;private SqlCommand cmd = null;private System.Windows.Forms.Button button2;private System.Windows.Forms.PictureBox pic1;private System.Windows.Forms
11、.Open open;private string sql = null;private System.Windows.Forms.Label label2;private string nowId=null;public Form1()/ Windows 窗體設計器支持所必需的/InitializeComponent();conn = new SqlConnection(ConnectionString);/ TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼/ < summary>/ 清理所有正在使用的資源。/ < /summary >
12、;protected override void Dispose( bool disposing )if (conn.State = ConnectionState.Open)conn.Close();if( disposing )if (components != null)components.Dispose();base.Dispose( disposing );#region Windows Form Designer generated code/ < summary>/ 設計器支持所需的方法 - 不要使用代碼編輯器修改/ 此方法的內(nèi)容。/ < /summary &
13、gt;private void InitializeComponent()this.button1 = new System.Windows.Forms.Button();this.pic1 = new System.Windows.Forms.PictureBox();this.button2 = new System.Windows.Forms.Button();this.open = new System.Windows.Forms.Open();this.label2 = new System.Windows.Forms.Label();this.SuspendLayout();/ b
14、utton1/this.button1.Location = new System.Drawing.Point(0, 40);this.button1.Name = "button1"this.button1.Size = new System.Drawing.Size(264, 48);this.button1.TabIndex = 0;this.button1.Text = " 加入新的圖片 "this.button1.Click += new System.EventHandler(this.button1_Click); / pic1/this.
15、pic1.Location = new System.Drawing.Point(280, 8);this.pic1.Name = "pic1"this.pic1.Size = new System.Drawing.Size(344, 264);this.pic1.TabIndex = 3;this.pic1.TabStop = false;/ button2/this.button2.Location = new System.Drawing.Point(0, 104); this.button2.Name = "button2"this.button
16、2.Size = new System.Drawing.Size(264, 40); this.button2.TabIndex = 4;this.button2.Text = " 從數(shù)據(jù)庫中恢復圖像 "this.button2.Click += new System.EventHandler(this.button2_Click); / open/this.open = "" 圖像文件 (*.jpg,*.bmp,*.gif)|*.jpg|*.bmp|*.gif""/ label2/this.label2.Location = new
17、 System.Drawing.Point(0, 152); this.label2.Name = "label2"this.label2.Size = new System.Drawing.Size(264, 48); this.label2.TabIndex = 5;/ Form1/this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(632, 273); this.Controls.AddRange(new System.Wi
18、ndows.Forms.Control this.label2, this.button2, this.pic1, this.button1);this.Name = "Form1" this.Text = "Form1"this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false);#endregion/ < summary>/ 應用程序的主入口點。/ < /summary > STAThread static void Main()
19、Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)open ();if (open()!="")fi = new (open);string imgtitle=open;int imgdatalen=(int)fi.Length;byte imgdata = new byteimgdatalen;Stream imgdatastream=fi.OpenRead();int n=imgdatastream.Read(imgdata,0,imgdata
20、len);if( conn.State = ConnectionState.Open) conn.Close();ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"DataSource=localhost;"conn.ConnectionString = ConnectionString;trystring mySelectQuery = "INSERT INTO ImageStore(imgtitle,imgdata) V A
21、LUES (imgtitle, imgdata )"/string mySelectQuery="UPDA TE ImageStore set imgtitle=imgtitle,imgdata=imgdata" ; SqlCommand myCommand = new SqlCommand(mySelectQuery, conn);SqlParameter paramTitle = new SqlParameter("imgtitle", SqlDbType.VarChar,50 ); paramTitle.Value = imgtitle;
22、myCommand.Parameters.Add( paramTitle);SqlParameter paramData = new SqlParameter( "imgdata", SqlDbType.Image ); paramData.Value = imgdata;myCommand.Parameters.Add( paramData );conn.Open();int numRowsAffected = myCommand.ExecuteNonQuery();conn.Close();catch(Exception err)MessageBox.Show(" 您 輸 入 名 稱 可 能 在 數(shù) 據(jù) 庫 中 已 存 在 或 輸 入 為 空 , 查!"+err.ToString() );finally private void Form1_Load(object sender, System.EventArgs e)private void button2_Click(object sender, System.EventArgs e)/打開數(shù)據(jù)庫連接if( conn.State = ConnectionState.Open) conn.Close();ConnectionString ="Integ
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 全案整裝合同范例
- 借款合同范本 個人
- 醫(yī)院保潔服務合同范本
- 五金合作合同范本
- 中介寄賣合同范本
- 單位廁所裝修合同范本
- 醫(yī)療家具清單購買合同范本
- 公司購買牛奶購銷合同范本
- 出租商用合同范本
- 十三薪標準合同范本
- 中山大學抬頭信紙中山大學橫式便箋紙推薦信模板a
- 皮膚性病學課件:濕疹皮炎
- 無形資產(chǎn)評估完整版課件
- 一體化學工服務平臺、人事管理系統(tǒng)、科研管理系統(tǒng)建設方案
- 市場營銷學課后習題與答案
- 常暗之廂(7規(guī)則-簡體修正)
- 10kV變電所設備檢修內(nèi)容與周期表
- 制冷系統(tǒng)方案的設計pptx課件
- 修心七要原文
- 中國TBHQ行業(yè)市場調(diào)研報告
- 1資產(chǎn)負債表變動情況的分析評價
評論
0/150
提交評論