Thursday, 29 November 2012
How Do You Get Money
For this purpose the SQL Server database provides a data type called "image" which is used to store images in the database. ASP.Net can be used to store images that are small to be stored in a database like SQL Server 2000 and later versions, net being the platform for distributed application now. . These images are usually stored in a web server folder and they are accessed by giving the relative path to the file with respect to the root folder of the website. Most of the web applications have a lot of images used in it.
Imaging are used in this task. And System.Drawing, System.IO, System.Data, System.Drawing, namespaces like System.Data.SqlClient. Use a button control to upload the image to the database. You can also create a textbox control in which you can add the image name or some comment or an image id for the image saved. Aspx page which can have a HTMLInputFile control which is used to select the image file that is to be saved in the database. You can create a , to find out how to insert and retrieve an image in to the SQL Server database. To access these images stored in the database we will be using the ADO.Net classes.
In the OnClick property of the button you can write the following code to upload an image to the database.
// create a byte[] for the image file that is uploaded
int imagelen = Upload.PostedFile.ContentLength;
byte[] picbyte = new byte[imagelen];
imagelen); 0, Upload.PostedFile.InputStream.Read (picbyte,
// Insert the image and image id into the database
"); . . SqlConnection conn = new SqlConnection (@"give the connection string here.
Try
{
conn.Open ();
picbyte); cmd.Parameters.Add ("@pic", conn); @imageid)", imageID) values (@pic, sqlCommand cmd = new SqlCommand ("insert into ImageTable "+ "(ImageField,
lblImageID.Text); cmd.Parameters.Add ("@imageid",
cmd.ExecuteNonQuery ();
}
Finally
{
conn.Close ();
}
The code given above performs the following steps in the process of inserting an image into the database. You can also write the above code in a function and call that function in the OnClick event of the upload button.
Get the content length of the image that is to be uploaded 1.
Create a byte[] to store the image 2.
Read the input stream of the posted file 3.
Create a connection object 4.
Open the connection object 5.
Create a command object 6.
Add parameters to the command object 7.
Execute the sql command using the ExecuteNonQuery method of the command object 8.
Close the connection object 9.
To retrieve the image from the SQL Database you can perform the following steps.
memoryStream mstream = new MemoryStream (); the code can be something like, Create a MemoryStream object. 1.
Create a Connection object 2.
Open the connection to the database 3.
Create a command object to execute the command to retrieve the image 4.
Use the command object's ExecuteScalar method to retrieve the image 5.
cast the output of the ExecuteScalar method to that of byte[]
byte[] image = (byte[]) command.ExecuteScalar (); 6.
image.Length); 0, write the stream mstream.Write (image, 7.
create a bitmap object to hold the stream Bitmap bitmap = new Bitmap (stream); 8.
set the content type to "image/gif" Response.ContentType = "image/gif"; 9.
ImageFormat.Gif); bitmap.Save (Response.OutputStream, Use the Save method of the bitmap object to output the image to the OutputStream. 10.
Close the connection 11.
close the stream mstream.Close(); 12.
Using the above steps you can retrieve and display the image from the database to the web page.
This method of storing avoids the tedious task of tracking the path of the web folder if the images are stored in a web folder. You can use these algorithms and take advantage of the "image" data type available in the SQLServer 2000 database to store small images that correspond to a particular record in the table of the database.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment