使用 Microsoft .NET 从 SQL Server 存储和检索图像






4.09/5 (75投票s)
2005 年 7 月 1 日
1分钟阅读

915133

20582
使用 Microsoft .NET 从 SQL Server 存储和检索图像。
引言
本文介绍如何使用 C# 在 Microsoft .NET 中从数据库存储和检索图像。
使用的工具
- SQL Server 2000
- Microsoft .NET 版本 1.1
- C#(基于 Windows 窗体应用程序)
存储图像
- 在 SQL Server 2000 数据库中创建一个表,该表至少包含一个类型为
IMAGE
的字段。这是我使用的脚本
CREATE TABLE [dbo].[tblImgData] ( [ID] [int] NOT NULL , [Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Picture] [image] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
- 实际上,
IMAGE
字段只是保存包含二进制数据的页面的引用,因此我们必须将图像转换为字节。- 我使用文件打开对话框来定位文件。
this.openFileDialog1.ShowDialog(this); string strFn=this.openFileDialog1.FileName;
- 通过使用
FileInfo
类,我检索了文件大小FileInfo fiImage=new FileInfo(strFn);
- 声明一个该大小的数组。
this.m_lImageFileLength=fiImage.Length; m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)];
- 通过使用
FileStream
对象,我填充了字节数组。FileStream fs=new FileStream(strFn,FileMode.Open, FileAccess.Read,FileShare.Read); int iBytesRead=fs.Read(m_barrImg,0, Convert.ToInt32(this.m_lImageFileLength)); fs.Close();
完整的加载图像代码
protected void LoadImage() { try { this.openFileDialog1.ShowDialog(this); string strFn=this.openFileDialog1.FileName; this.pictureBox1.Image=Image.FromFile(strFn); FileInfo fiImage=new FileInfo(strFn); this.m_lImageFileLength=fiImage.Length; FileStream fs=new FileStream(strFn,FileMode.Open, FileAccess.Read,FileShare.Read); m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)]; int iBytesRead = fs.Read(m_barrImg,0, Convert.ToInt32(this.m_lImageFileLength)); fs.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message); } }
- 我使用文件打开对话框来定位文件。
- 将字节数组数据保存到数据库。
- 创建命令文本以插入记录。
this.sqlCommand1.CommandText= "INSERT INTO tblImgData(ID,Name,Picture)" + " values(@ID,@Name,@Picture)";
- 创建参数。
this.sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4); this.sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 50); this.sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image);
请注意,“
@Picture
”具有“SqlDbType.Image
”,因为它是一个IMAGE
类型字段。 - 为参数提供值。
this.sqlCommand1.Parameters["@ID"].Value=this.editID.Text; this.sqlCommand1.Parameters["@Name"].Value=this.editName.Text; this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg;
“
this.m_barrImg
”是一个我们在上一步中填充的字节数组。 - 现在执行非查询以将记录保存到数据库。
int iresult=this.sqlCommand1.ExecuteNonQuery();
完整的保存图像代码
private void btnSave_Click(object sender, System.EventArgs e) { try { this.sqlConnection1.Open(); if (sqlCommand1.Parameters.Count ==0 ) { this.sqlCommand1.CommandText="INSERT INTO tblImgData(ID," + " Name,Picture) values(@ID,@Name,@Picture)"; this.sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int,4); this.sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar,50); this.sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image); } this.sqlCommand1.Parameters["@ID"].Value=this.editID.Text; this.sqlCommand1.Parameters["@Name"].Value=this.editName.Text; this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg; int iresult=this.sqlCommand1.ExecuteNonQuery(); MessageBox.Show(Convert.ToString(iresult)); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { this.sqlConnection1.Close(); } }
- 创建命令文本以插入记录。
检索图像
从数据库检索图像是保存图像到数据库的完全相反的过程。
- 首先创建命令文本以检索记录。
SqlCommand cmdSelect = new SqlCommand("select Picture" + " from tblImgData where ID=@ID", this.sqlConnection1);
- 为查询创建参数。
cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
- 为参数提供值。
cmdSelect.Parameters["@ID"].Value=this.editID.Text;
- 打开数据库连接并执行“
ExecuteScalar
”,因为我们只想获取“IMAGE
”列数据。byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();
由于 execute scalar 返回“
Object
”数据类型的数据,我们将其转换为byte
数组。 - 将此数据保存到临时文件。
string strfn=Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs=new FileStream(strfn,FileMode.CreateNew,FileAccess.Write); fs.Write(barrImg,0,barrImg.Length); fs.Flush(); fs.Close();
- 并在您想要显示图像的任何地方显示图像。
pictureBox1.Image=Image.FromFile(strfn);
完整的图像检索代码
private void btnLoad_Click(object sender, System.EventArgs e)
{
try
{
SqlCommand cmdSelect=new SqlCommand("select Picture" +
" from tblImgData where ID=@ID",this.sqlConnection1);
cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
cmdSelect.Parameters["@ID"].Value=this.editID.Text;
this.sqlConnection1.Open();
byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();
string strfn=Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs=new FileStream(strfn,
FileMode.CreateNew, FileAccess.Write);
fs.Write(barrImg,0,barrImg.Length);
fs.Flush();
fs.Close();
pictureBox1.Image=Image.FromFile(strfn);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.sqlConnection1.Close();
}
}