您的位置:首页 > 数据库

使用.NET从SQL服务器保存和读取图片

2008-04-16 09:56 295 查看
用SQL Server来保存图片的程序
介绍:
这片文章是关于使用.NET从SQL服务器保存和读取图片。
使用工具:
SQL Server 2000
Microsoft .NET Version 1.1
C# (Windows Forms based application)
保存图片:
在一个SQL Server 2000 数据库中建立一个表至少要有一个IMAGE类型的字段。
这里用到的SQL语句如下:
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字段只是保存了包含二进制数据参数的字段,所以我们必须将图片转化成字节型。
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);
}
}

保存字节数组数据到数据库。
Create command text to insert record. this.sqlCommand1.CommandText=
"INSERT INTO tblImgData(ID,Name,Picture)" +
" values(@ID,@Name,@Picture)";

Create parameters. 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”是一个字节数组,刚才我们已经填充过了。
现在执行SQL用来保存记录到数据库中:
int iresult=this.sqlCommand1.ExecuteNonQuery();

完成保存图片代码:
Collapseprivate 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();

通过分阶段的运行指令返回“Object”类型的数据,我们将其转换为字节数组。
保存数据到临时文件中:
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐