GOCHESTER.COM
Search:
 
 
Membership
 
  Member Login
Become a Member
Reset Password
 
     

 
Site Links
 
  Index
News Articles
View Articles By Category
 
     

 
Saving and Retreiving Images from Sql Server 2005
Category: Database related articles
By: Chester Zhang - August 22nd, 2008
This article has been read: 87 times.



Hai Every One,

I have written the code for saving and retrieving images from Sql server 2005. Images are saved into database in binary format. But when I'm retreing, am getting error. This is the code

CREATE TABLE [dbo].[PictureTable]
(
[ImageID] [int] IDENTITY(1,1) NOT NULL,
[Title] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DateAdded] [datetime] NOT NULL,
[MIMEType] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Image] [image] NOT NULL
)


This is for inserting images into database.


ImagesIntoDatabase.aspx.cs

protected void Upload_Click(object sender, EventArgs e)
{
FileUpload fileUpload1 = (FileUpload)this.FindControl("fileUpload1");
//Make sure a file has been successfully uploaded
if (fileUpload1.PostedFile == null || string.IsNullOrEmpty(fileUpload1.PostedFile.FileName) || fileUpload1.PostedFile.InputStream == null)
{
Label1.Text = "Please Upload Valid picture file";
return; // TODO: might not be correct. Was : Exit Sub
}
//Make sure we are dealing with a JPG or GIF file
string extension = System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName).ToLower();
string MIMEType = null;
switch (extension)
{
case ".gif":
MIMEType = "image/gif";
break;
case ".jpg":
case ".jpeg":
case ".jpe":
MIMEType = "image/jpeg";
break;
case ".png":
MIMEType = "image/png";
break;
default:
//Invalid file type uploaded
Label1.Text = "Not a Valid file format";
return; // TODO: might not be correct. Was : Exit Sub

break;
}
//Connect to the database and insert a new record into Products
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ExamplesConnectionString"].ConnectionString))
{
const string SQL = "INSERT INTO [PictureTable] ([Title], [MIMEType], [Image],[DateAdded]) VALUES (@Title, @MIMEType, @ImageData,@DateAdded)";
SqlCommand myCommand = new SqlCommand(SQL, myConnection);
myCommand.Parameters.AddWithValue("@Title", TextBox1.Text.Trim());
myCommand.Parameters.AddWithValue("@MIMEType", MIMEType);
myCommand.Parameters.AddWithValue("@DateAdded", DateTime.Now);
//Load FileUpload's InputStream into Byte array
byte[] imageBytes = new byte[fileUpload1.PostedFile.InputStream.Length + 1];
fileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
myCommand.Parameters.AddWithValue("@ImageData", imageBytes);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}

This is for retrieving images from database

DisplayPictures.aspx.cs


protected void Button1_Click(object sender, EventArgs e)
{

int ImageID = Convert.ToInt32(TextBox1.Text);

//Connect to the database and bring back the image contents & MIME type for the specified picture
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ExamplesConnectionString"].ConnectionString))
{

const string SQL = "SELECT [MIMEType], [Image] FROM [PictureTable] WHERE [ImageID] = @ImageID";
SqlCommand myCommand = new SqlCommand(SQL, myConnection);
myCommand.Parameters.AddWithValue("@ImageID", ImageID);

myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();

if (myReader.Read)
{
Response.ContentType = myReader("MIMEType").ToString();
Response.BinaryWrite(myReader("Image"));
}
myReader.Close();
myConnection.Close();
}

}



When I run displaypictures.aspx page, I'm getting error. Anyone help mee

Thanks in Advance.




I think there is a syntax error in your code.
A couple of parenthesis are missing behind the "myReader.Read", it should be a "bool" here not a method type.

if (myReader.Read) -> if (myReader.Read())


Back to News Articles
View Articles From Database related articles Category