How to store image file in Sql server database?
Answers were Sorted based on User's Feedback
//use this code to store image file in sql server database
//get directory Path(C:\\Temp) where image has stored and
use these two method in ur program
private void GetImagePath(string strDirectoryPath)
{
DirectoryInfo mobjDirInfo = new DirectoryInfo
(strDirectoryPath);
//object[] mobjStore = new object
[Directory.GetFiles(strDirectoryPath, "*.JPG").Length];
int counter =1;
foreach (FileInfo fl in mobjDirInfo.GetFiles
("*.JPG"))
{
// Create a new stream to load this photo
into
FileStream stream = new FileStream
(fl.FullName.ToString(), FileMode.Open, FileAccess.Read);
// Create a buffer to hold the stream bytes
byte[] buffer = new byte[stream.Length];
// Read the bytes from this stream
stream.Read(buffer, 0, (int)stream.Length);
// Now we can close the stream
stream.Close();
// Extract out the name of the file an use
it for the name of the photo
string strName =
Path.GetFileNameWithoutExtension(fl.Name.ToString());
// Insert the image into the database and
add it to the tree
InsertImageIntoDatabase(ref buffer,
strName, counter);
buffer = null;
counter = counter+1;
}
}
//Insert Images into sql server (create table TBLIMAGE
(PHOTOID int,PHOTONAME varchar(20),PHOTO IMAGE))
private void InsertImageIntoDatabase(ref byte[]
buffer, string strPhotoName, int intPhotoid)
{
if (mobjConn.State == ConnectionState.Closed)
{
mobjConn.Open();
}
mobjCmd = new SqlCommand("Insert Into TBLIMAGE
values(@PHOTOID,@PHOTONAME,@PHOTO)", mobjConn);
mobjCmd.Parameters.Add("@PHOTOID", intPhotoid);
mobjCmd.Parameters.Add
("@PHOTONAME",strPhotoName);
mobjCmd.Parameters.Add("@PHOTO", buffer);
mobjCmd.ExecuteNonQuery();
}
//if u have any query please revert back
Is This Answer Correct ? | 9 Yes | 1 No |
Answer / gp_bellamkonda
=====Here is the sample for image storage and retrieve===
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];
Upload.PostedFile.InputStream.Read (picbyte, 0, imagelen);
// Insert the image and image id into the database
SqlConnection conn = new SqlConnection (@"give the
connection string here...");
try
{
conn.Open ();
SqlCommand cmd = new SqlCommand ("insert into ImageTable "
+ "(ImageField, ImageID) values (@pic, @imageid)", conn);
cmd.Parameters.Add ("@pic", picbyte);
cmd.Parameters.Add ("@imageid", lblImageID.Text);
cmd.ExecuteNonQuery ();
}
finally
{
conn.Close ();
}
You can also write the above code in a function and call
that function in the OnClick event of the upload button.
The code given above performs the following steps in the
process of inserting an image into the database.
1. Get the content length of the image that is to be
uploaded
2. Create a byte[] to store the image
3. Read the input stream of the posted file
4. Create a connection object
5. Open the connection object
6. Create a command object
7. Add parameters to the command object
8. Execute the sql command using the ExecuteNonQuery method
of the command object
9. Close the connection object
To retrieve the image from the SQL Database you can perform
the following steps.
1. Create a MemoryStream object. The code can be something
like,
MemoryStream mstream = new MemoryStream ();
2. Create a Connection object
3. Open the connection to the database
4. Create a command object to execute the command to
retrieve the image
5. Use the command object’s ExecuteScalar method to
retrieve the image
6. Cast the output of the ExecuteScalar method to that of
byte[]
byte[] image = (byte[]) command.ExecuteScalar ();
7. Write the stream
mstream.Write (image, 0, image.Length);
8. Create a bitmap object to hold the stream
Bitmap bitmap = new Bitmap (stream);
9. Set the content type to “image/gif”
Response.ContentType = "image/gif";
10. Use the Save method of the bitmap object to output the
image to the OutputStream.
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
11. Close the connection
12. Close the stream
mstream.Close();
Using the above steps you can retrieve and display the
image from the database to the web page.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / guest
Yes, you can store image files in SQL Server. In fact, SQL
Server has a datatype called image for exactly this
purpose. It will store any binary data up to 2 GB in size.
There are many ways to get image files into your image
column. Among the easiest is the Textcopy utility located
in the Program FilesMicrosoft SQL ServerMSSQLBinn
directory. Use the command "textcopy /?" to get the command
line syntax for this utility.
Another way to get image files in and out of SQL Server is
to write your own program using ADO or ADO.Net, or create
an ActiveX dll that can be called by SQL Server's sp_OA*
extended stored procedures.
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / y.s.a.naidu
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,
pictureBox1.Image.RawFormat);
byte[] img = ms.GetBuffer();
SqlCommand cmd = new SqlCommand("Insert into
tbl_Image(Images) values(@imgs)", cn);
SqlParameter p1 = new SqlParameter();
p1.ParameterName = "@imgs";
p1.SqlDbType = SqlDbType.Image;
p1.Value = img;
cmd.Parameters.Add(p1);
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Image Inserted ....");
cn.Close();
Is This Answer Correct ? | 1 Yes | 3 No |
Can you allow class to be inherited, but prevent the method from being over-ridden?
how to compare numbers and dispaly the largest ? *first thing I wanted to do is to input how many numbers to be compared *and then analyzed the largest then display it.
How do I do a case-insensitive string comparison?
How is a strongly-named assembly different from one that isn’t strongly-named?
What is concrete class in c# with example?
Difference between value and reference type.
Is multilevel inheritance possible in c#?
What are partial types in c#?
What is the and operator in c#?
One start and stop button is there and one textbox is there.when you click start button ,it will display 1,2,3......up to infinite in textbox.when you click stop button it will stop.
What is namespace in oops?
What is the difference between Singleton design pattern and Factory design pattern?