該例子是一個對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”