国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 編程應(yīng)用 > 正文

ASP.NET如何存取SQL Server數(shù)據(jù)庫圖片

2019-11-18 16:58:24
字體:
供稿:網(wǎng)友

  SQL Server提供了一個(gè)特別的數(shù)據(jù)類型:image,它是一個(gè)包含binary數(shù)據(jù)的類型。下邊這個(gè)例子就向你展示了如何將文本或照片放入到數(shù)據(jù)庫中的辦法。在這篇文章中我們要看到如何在SQL Server中存儲(chǔ)和讀取圖片。

  1、建立一個(gè)表:

  在SQL SERVER中建立這樣結(jié)構(gòu)的一個(gè)表:

  

ASP.NET如何存取SQLServer數(shù)據(jù)庫圖片

  2、存儲(chǔ)圖片到SQL SERVER數(shù)據(jù)庫中

  為了能存儲(chǔ)到表中,你首先要上傳它們到你的WEB 服務(wù)器上,你可以開發(fā)一個(gè)web form,它用來將客戶端中TextBox web control中的圖片入到你的WEB服務(wù)器上來。將你的 encType 屬性設(shè)置為:myltipart/formdata.

  Stream imgdatastream = File1.PostedFile.InputStream;

  int imgdatalen = File1.PostedFile.ContentLength;

  string imgtype = File1.PostedFile.ContentType;

  string imgtitle = TextBox1.Text;

  byte[] imgdata = new byte[imgdatalen];

  int n = imgdatastream.Read(imgdata,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 )", 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( 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ù)庫中恢復(fù)讀取

  現(xiàn)在讓我們來從SQL Server中讀取我們放入的數(shù)據(jù)吧!我們將要輸出圖片到你的瀏覽器上,你也可以將它存放到你要的位置。

  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 = " + 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"] );

  }

  connection.Close();

  }

  要注意的是Response.BinaryWrite 而不是Response.Write.

  下面給大家一個(gè)用于C# Winform的存入、讀取程序。其中不同請(qǐng)大家自己比較!(為了方便起見,我將數(shù)據(jù)庫字段簡(jiǎn)化為二個(gè):imgtitle和imgdata。

  using System;

  using System.Drawing;

  using System.Collections;

  using System.ComponentModel;

  using System.Windows.Forms;

  using System.Data;

  using System.IO;

  using System.Data.SqlClient;

  namespace Windowsapplication21

  {

  ///

  /// Form1 的摘要說明。

  ///

  public class Form1 : System.Windows.Forms.Form

  {

  private System.Windows.Forms.Button button1;

  ///

  /// 必需的設(shè)計(jì)器變量。

  ///

  private System.ComponentModel.Container components = null;

  private string ConnectionString = "Integrated Security=SSPI;Initial Catalog=;DataSource=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.OpenFileDialog openFileDialog1;

  private string sql = null;

  private System.Windows.Forms.Label label2;

  private string nowId=null;

  public Form1()

  {

  //

  // Windows 窗體設(shè)計(jì)器支持所必需的

  //

  InitializeComponent();

  conn = new SqlConnection(ConnectionString);

  //

  // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

  //

  }

  ///

  /// 清理所有正在使用的資源。

  ///

  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

  ///

  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改

  /// 此方法的內(nèi)容。

  ///

  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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();

  this.label2 = new System.Windows.Forms.Label();

  this.SuspendLayout();

  //

  // button1

  //

  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.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.button2.Size = new System.Drawing.Size(264, 40);

  this.button2.TabIndex = 4;

  this.button2.Text = "從數(shù)據(jù)庫中恢復(fù)圖像";

  this.button2.Click += new System.EventHandler(this.button2_Click);

  //

  // openFileDialog1

  //

  this.openFileDialog1.Filter = ""圖像文件(*.jpg,*.bmp,*.gif)|*.jpg|*.bmp|*.gif"";

  //

  // label2

  //

  this.label2.Location = new 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.Windows.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

  ///

  /// 應(yīng)用程序的主入口點(diǎn)。

  ///

  [STAThread]

  static void Main()

  {

  Application.Run(new Form1());

  }

  private void button1_Click(object sender, System.EventArgs e)


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长武县| 江孜县| 阳曲县| 伊金霍洛旗| 周至县| 中宁县| 竹山县| 偃师市| 鲁甸县| 祁东县| 耒阳市| 沽源县| 兰溪市| 南和县| 莫力| 大宁县| 岑巩县| 鄂托克旗| 阜宁县| 宣化县| 聂荣县| 濉溪县| 平遥县| 泊头市| 深圳市| 岚皋县| 开平市| 马关县| 临夏市| 东阳市| 秭归县| 古田县| 怀集县| 光泽县| 海伦市| 江华| 东丰县| 改则县| 汨罗市| 榆林市| 肇州县|