麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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

2019-11-18 16:58:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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

  

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

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

  為了能存儲(chǔ)到表中,你首先要上傳它們到你的WEB 服務(wù)器上,你可以開(kāi)發(fā)一個(gè)web form,它用來(lái)將客戶端中TextBox web control中的圖片入到你的WEB服務(wù)器上來(lái)。將你的 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ù)庫(kù)中恢復(fù)讀取

  現(xiàn)在讓我們來(lái)從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)大家自己比較!(為了方便起見(jiàn),我將數(shù)據(jù)庫(kù)字段簡(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 的摘要說(shuō)明。

  ///

  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ù)庫(kù)中恢復(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ā)表
主站蜘蛛池模板: 少妇一级淫片免费放正片 | 夜夜看 | 精品一区二区三区四区在线 | 午夜在线观看视频网站 | 法国性经典xxxhd | 成年人在线视频 | 国产中文av在线 | 欧美日韩一区,二区,三区,久久精品 | 91精品国产91久久久久久吃药 | 国产91久久精品一区二区 | 国产1区2区3区中文字幕 | 精品在线免费播放 | 久草久视频 | 羞羞网站在线看 | 91麻豆精品国产91久久久无需广告 | 国产精品久久久久国产精品三级 | 国产女同玩人妖 | 免费久久久久 | 国产午夜网 | 视频一区二区久久 | 成人一区二区三区四区 | 久久爽精品区穿丝袜 | 成人免费一区二区三区在线观看 | 黄色99视频 | 日韩激情| 国产美女视频一区 | 国产精品久久久久久久久久 | 亚洲码无人客一区二区三区 | 成年人福利视频 | 91精品一区二区综合在线 | 欧美成人精品欧美一级乱黄 | 十级毛片| 久久国产亚洲精品 | 久久精品视频69 | 国产免费一级淫片a级中文 99国产精品自拍 | 久久久久久久久国产 | 蜜桃精品视频在线观看 | 欧美伦交| 成人一级黄色大片 | 欧美一级片 在线播放 | 久久久久久久久久久国产精品 |