Register  |  Login



Question

Status: Closed Points: 125 Time: 00:44 - Dec 27, 2008  

priyaravi2008

how to store images in sqlserver using asp.net?

store images in sqlserver using asp.net?please give example

Categories

Answer Discussion
Tutorials

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

shashi.programmer

Date:: Dec 27, 2008

Time:: 00:49

#####Saving Photo in SQL Server Using SqlClient

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
using (BinaryReader reader = new BinaryReader
(FileUpload1.PostedFile.InputStream))
{
byte[] image = reader.ReadBytes
(FileUpload1.PostedFile.ContentLength);
SaveImage(image);
}
}
}

private int SaveImage(byte[] image)
{
int rowsAffected;

using (SqlConnection connection = new SqlConnection(“...“))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO Photos
(Photo) VALUES (@Photo)";
command.Parameters.AddWithValue("@Photo", image);

connection.Open();
rowsAffected = command.ExecuteNonQuery();
}
}

return rowsAffected;
}



######Display Photo In SQL Server Using ASP.NET

protected void Page_Init(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
int id;

if (Int32.TryParse(Request.QueryString["id"], out id))
{
Response.Clear();
Response.ContentType = "image/jpeg";

Image image = RetrieveImage(id);
image.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}
}

private Image RetrieveImage(int photoId)
{
Image image = null;

using (SqlConnection connection = new SqlConnection("..."))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT Photo FROM Photos
WHERE PhotoId = @PhotoId";
command.Parameters.AddWithValue("@PhotoId", photoId);

connection.Open();
byte[] imageData = (byte[])command.ExecuteScalar();

MemoryStream memStream = new MemoryStream(imageData);
image = Image.FromStream(memStream);
}
}

return image;
}





###### OR #########################
Code in the OnClick event of the Submit button

Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream

' Gets the Size of the Image
intImageSize = PersonImage.PostedFile.ContentLength

' Gets the Image Type
strImageType = PersonImage.PostedFile.ContentType

' Reads the Image
ImageStream = PersonImage.PostedFile.InputStream

Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As New SqlCommand("sp_person_isp", myConnection)

' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)
prmPersonImage.Value = ImageContent
myCommand.Parameters.Add(prmPersonImage)

Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)
prmPersonImageType.Value = strImageType
myCommand.Parameters.Add(prmPersonImageType)

Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Response.Write("New person successfully added!")
Catch SQLexc As SqlException
Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
End Try

Question Answered

This question has been closed, and points have been rewarded to the following experts:


shashi.programmer: 125

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

9829

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4773

5.

PeterNZ

3487

6.

gonzalo

2765

7.

Mason

2520

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es