Hi Frndz,
Functionality: Insert Image into DB Table
Store Image in Datatable in Binary Format
Take one FileUpload Control, One Upload Button
Create DB table with Primary kEy Auto Incremnt.
On Upload Button Click event Call Following logic
Logic:
Create Table
CREATE TABLE [dbo].[tblImage](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[image] [image] NULL,
CONSTRAINT [PK_tblImage] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Upload Button Click Event
protected void btnUpload_Click(object sender, EventArgs e)
{
//Condition to check if the file uploaded or not
if (fileuploadImage.HasFile)
{
//getting length of uploaded file
int length = fileuploadImage.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = fileuploadImage.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
//Here call InsertImage Function for store ImageName and Image in Binary Foramt
InsertImage(fileuploadImage.FileName.ToString(), imgbyte);
}
}
Function for Insert Image in DB Table
private void InsertImage(string imagename, byte[] imagebyte)
{
string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
string strQuery = "Insert into tblImage(Name,Image) values(@Name,@Image) ";
cmd.CommandText = strQuery;
SqlParameter Name = new SqlParameter("@Name", SqlDbType.VarChar, 50);
Name.Value = imagename;
cmd.Parameters.Add(Name);
SqlParameter Image = new SqlParameter("@Image", SqlDbType.Image);
Image.Value = imagebyte;
cmd.Parameters.Add(Image);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Hope this helpful!
Thanks