In this blog we are going to learn how to
store an image into a database ( SQL Server 2008), after retrieving it from
System File.
Following application s should be used
- SQL SERVER 2008.
- MICROSOFT VISUAL STUDIO 2010.
Steps to be followed in SQL SERVER 2008
- Create a database
- Create a table
Here, I created the table with the following
columns
- Picture_ID
- Description
- Picture_Details
While Creating the table choose the
appropriate 'Datatype'.
Steps to be followed in Microsoft Visual Studio 2010
First ,We have to Create an Windows Form Application.
![Image-2.jpg]()
Tools to be used:
- Button
- Picture box
- Label
- Text box
Command should be used
- OpenFileDialog
Code to Open the Image from the System file:
// Select the
Image(s) from System File(s)
private
void btnBrowse_Click(object
sender, EventArgs e)
{
OpenFileDialog open =
new OpenFileDialog();
//
Fiter,used to Filter the image in the required filetype(s)
open.Filter =
Image
Files(*.jpg;*.jpeg;*.gif;*.bmp)|*.jpg;*.jpeg;*.gif;*.bmp";
if
(open.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image =
new Bitmap(open.FileName);
txtPathdirectory.Text
= open.FileName;
}
}
//After Opening the Image from the
System File
![Image-3.jpg]()
Code to Insert an Image into the Database:
//Inserting an Image into the Database
private void btninsert_Click(object
sender, EventArgs e)
{
SqlConnection objcon =
null;
SqlCommand objcmd =
null;
using (objcon = new
SqlConnection("server=PC250772;initial
catalog=image;user id=sa;password=password-1"))
{
try
{
objcon.Open();
string query =
"insert into imageinfo (Picture_Details)values('" + txtPathdirectory.Text
+ "')";
objcmd =
new SqlCommand(query,
objcon);
int rowaffected = objcmd.ExecuteNonQuery();
MessageBox.Show("Inserted Successfully");
MessageBox.Show(rowaffected + "row'(s)" +
"affected");
}
catch (Exception
ex)
{
MessageBox.Show(ex.Message);
}
}
}
//After Inserting an Image into the
Database
![Image-4.jpg]()
//Result in SQL SERVER after Inserting
the Image
![Image-5.jpg]()