程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#操作SQL Server中的Image類型數據

C#操作SQL Server中的Image類型數據

編輯:C#入門知識

該例子是一個對SQL Server數據類型的一個操作例子,具有寫入、讀取功能。

1:准備數據庫

1)創建數據庫 Test

2)創建表 Table_1 (分別有2個字段:id(Int)、photo(Image))

如圖:

 \

 2:用C#進行讀寫操作,完整代碼如下:


view plaincopy to clipboardprint?
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.IO; 
 
namespace imageTest 

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void btn_brewse_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog op = new OpenFileDialog(); 
            op.Title = "浏覽圖像文件"; 
            op.Filter = "圖像文件(*.jpg)|*.jpg"; 
            op.ShowDialog(); 
            txt_ImageAddress.Text = op.FileName; 
        } 
 
        private void btn_Insert_Click(object sender, EventArgs e) 
        { 
            FileStream fs = new FileStream(txt_ImageAddress.Text,FileMode.Open,FileAccess.Read); 
            byte[] byteArray = new byte[fs.Length]; 
            fs.Read(byteArray,0,Convert.ToInt32(fs.Length)); 
            fs.Close(); 
 
            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456"; 
            SqlConnection conn = new SqlConnection(connectionStr); 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand("INSERT INTO Table_1(photo) VALUES(@photo)",conn); 
            SqlParameter parmeter = new SqlParameter("@photo", SqlDbType.Image); 
            parmeter.Value = byteArray; 
            cmd.Parameters.Add(parmeter); 
            int result = cmd.ExecuteNonQuery(); 
            if (result > 0) 
                MessageBox.Show("插入成功"); 
            conn.Close(); 
        } 
 
        private void btn_ReadImage_Click(object sender, EventArgs e) 
        { 
            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456"; 
            SqlConnection conn = new SqlConnection(connectionStr); 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand("SELECT * FROM Table_1",conn); 
            SqlDataReader dr = cmd.ExecuteReader(); 
            dr.Read(); 
            byte[] image = (byte[])dr["photo"]; 
            conn.Close(); 
            if (image.Length == 0) 
                return; 
            string photoUrl = Environment.CurrentDirectory + "\\1.jpg"; 
            FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate, FileAccess.Write); 
            BinaryWriter bw = new BinaryWriter(fs); 
            bw.BaseStream.Write(image, 0, image.Length); 
            bw.Flush(); 
            bw.Close(); 
 
            picbox_image.ImageLocation = photoUrl; 
        } 
 
    } 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace imageTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_brewse_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "浏覽圖像文件";
            op.Filter = "圖像文件(*.jpg)|*.jpg";
            op.ShowDialog();
            txt_ImageAddress.Text = op.FileName;
        }

        private void btn_Insert_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(txt_ImageAddress.Text,FileMode.Open,FileAccess.Read);
            byte[] byteArray = new byte[fs.Length];
            fs.Read(byteArray,0,Convert.ToInt32(fs.Length));
            fs.Close();

            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
            SqlConnection conn = new SqlConnection(connectionStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Table_1(photo) VALUES(@photo)",conn);
            SqlParameter parmeter = new SqlParameter("@photo", SqlDbType.Image);
            parmeter.Value = byteArray;
            cmd.Parameters.Add(parmeter);
            int result = cmd.ExecuteNonQuery();
            if (result > 0)
                MessageBox.Show("插入成功");
            conn.Close();
        }

        private void btn_ReadImage_Click(object sender, EventArgs e)
        {
            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
            SqlConnection conn = new SqlConnection(connectionStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Table_1",conn);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            byte[] image = (byte[])dr["photo"];
            conn.Close();
            if (image.Length == 0)
                return;
            string photoUrl = Environment.CurrentDirectory + "\\1.jpg";
            FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.BaseStream.Write(image, 0, image.Length);
            bw.Flush();
            bw.Close();

            picbox_image.ImageLocation = photoUrl;
        }

    }
}

 

 

效果如圖:

 \

作者“Andrew's Blog”
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved