![]() |
Database »
Database »
SQL Server
Intermediate
Storing and Retrieving Images from SQL Server using Microsoft .NETBy Kazim Sardar MehdiStoring and retrieving images from SQL Server using Microsoft .NET. |
C#, SQL, Windows, .NET 1.1, ADO.NET, SQL 2000, VS.NET2003, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article is about storing and retrieving images from database in Microsoft .NET using C#.
IMAGE.
Here is the script I used:
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 field is just holding the reference to the page containing the binary data so we have to convert our image into bytes.
this.openFileDialog1.ShowDialog(this);
string strFn=this.openFileDialog1.FileName;
FileInfo class, I retrieved the file size: FileInfo fiImage=new FileInfo(strFn);
this.m_lImageFileLength=fiImage.Length;
m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)];
FileStream object, I filled the byte array. 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);
Notice �@Picture� has �SqlDbType.Image� because it is of IMAGE type Field.
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� is a byte array which we filled in the previous step.
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();
}
}Retrieving images from the database is the exact reverse process of saving images to the database.
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� because we want only �IMAGE� column data back. byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();
As the execute scalar returns data of �Object� data type, we cast it to byte array.
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();
}
}
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Jun 2005 Editor: Smitha Vijayan |
Copyright 2005 by Kazim Sardar Mehdi Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |