How to store image file in Sql server database?

Answers were Sorted based on User's Feedback



How to store image file in Sql server database?..

Answer / amit kumar sharma

//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

How to store image file in Sql server database?..

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

How to store image file in Sql server database?..

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

How to store image file in Sql server database?..

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

Post New Answer

More C Sharp Interview Questions

How does a while loop work?

0 Answers  


Can hashset contain duplicates c#?

0 Answers  


Write a program in C# for checking a given number is PRIME or not.

0 Answers  


Can I call a virtual method from a constructor/destructor?

0 Answers  


How to store image file in Sql server database?

4 Answers   Unique Software Systems,






Is datetime a value type in c#?

0 Answers  


What is response redirect in c#?

0 Answers  


What is void in c#?

0 Answers  


Why dictionary is used in c#?

0 Answers  


what is IFormatable

0 Answers   Wipro,


What is the difference between yield and return?

0 Answers  


Valuetype/reference type?

6 Answers   Microsoft,


Categories