Save Image in DataBase in .NET

Santosh4u
Posted by Santosh4u under VB.NET category on | Views : 2282
Below Code will help u to insert image in DataBase
VB CODE
Dim ImagePath As String = "F:\santosh\Image"
Dim ImageInput As String = "Sample.jpg"
Dim ImageOutput As String = "SampleOut.jpg"
Dim dtReturnImage As DataTable
Shared StrConn As String = ""
Private Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveImage.Click
Dim ReturnValue As Integer = 0
Dim Img As FileStream
Dim BinaryReader As BinaryReader
Dim ImgArray() As Byte
Img = New FileStream(ImagePath & "\" & ImageInput, FileMode.Open, FileAccess.Read)
BinaryReader = New BinaryReader(Img)
ImgArray = BinaryReader.ReadBytes(Img.Length)
BinaryReader.Close()
Img.Close()
Dim cls As New clsCommon
ReturnValue = InsertImage(0, ImgArray)
If ReturnValue > 0 Then
Dim dt As New DataTable
dt = cls.ReturnImage(ReturnValue)
For Each dr As DataRow In dt.Rows
ImgArray = CType(dr("ImageFile"), Byte())
Dim oOutput As FileStream = File.Create(ImagePath & "\" & ImageOutput, ImgArray.Length)
oOutput.Write(ImgArray, 0, ImgArray.Length)
oOutput.Close()
Next
End If
End Sub
Private Function InsertImage(ByVal ImageID As Integer, ByVal Image() As Byte) As Integer
Dim InsertSqlCommand As New SqlCommand()
Dim RetunId As Integer = 0
Dim com As New SqlCommand
Dim con As New SqlConnection
con = GetConnectObj()
com.Connection = con
com.CommandType = CommandType.StoredProcedure
com.CommandText = "SaveImage"
Dim objOutputParam As New SqlParameter("@NewID", SqlDbType.Int)
objOutputParam.Direction = ParameterDirection.Output
Dim objImageParam As New SqlParameter("@ImageFile", SqlDbType.Image)
objImageParam.Direction = ParameterDirection.Input
objImageParam.Value = Image
Dim objImageIDParam As New SqlParameter("@ImageID", SqlDbType.Int, 12, ParameterDirection.Input)
objImageIDParam.Value = ImageID
com.Parameters.Add(objImageIDParam)
com.Parameters.Add(objImageParam)
com.Parameters.Add(objOutputParam)
con.Open()
com.ExecuteNonQuery()
RetunId = objOutputParam.Value
Return RetunId
End Function
Public Function ReturnImage(ByVal ImageIdvalue As Integer) As DataTable
dtReturnImage = FillDatatable(" select ImageFile from Images where ImageID = " & ImageIdvalue & "")
Return dtReturnImage
End Function
Private Function ConnectionString() As String
StrConn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Santosh_Practice;Data Source=SANTOSH\SQLEXPRESS" 'Myconnection is Key in App.Config
Return StrConn
End Function
Private Function GetConnectObj() As SqlConnection
Return New SqlConnection(ConnectionString())
End Function
Private Function FillDatatable(ByVal Query As String) As DataTable
Dim objDT As New DataTable()
Dim objda As New SqlDataAdapter(Query, ConnectionString())
objda.Fill(objDT)
Return objDT
End Function

C# CODE
private string ImagePath = "F:\\santosh\\Image";
private string ImageInput = "Sample.jpg";
private string ImageOutput = "SampleOut.jpg";
private DataTable dtReturnImage;
public static string StrConn = "";
private void btnSaveImage_Click(object sender, System.EventArgs e)
{
int ReturnValue = 0;
FileStream Img = null;
BinaryReader BinaryReader = null;
byte[] ImgArray = null;
Img = new FileStream(ImagePath + "\\" + ImageInput, FileMode.Open, FileAccess.Read);
BinaryReader = new BinaryReader(Img);
ImgArray = BinaryReader.ReadBytes(Img.Length);
BinaryReader.Close();
Img.Close();
clsCommon cls = new clsCommon();
ReturnValue = InsertImage(0, ImgArray);
if (ReturnValue > 0)
{
DataTable dt = new DataTable();
dt = cls.ReturnImage(ReturnValue);
foreach (DataRow dr in dt.Rows)
{
ImgArray = (byte[])(dr["ImageFile"]);
FileStream oOutput = File.Create(ImagePath + "\\" + ImageOutput, ImgArray.Length);
oOutput.Write(ImgArray, 0, ImgArray.Length);
oOutput.Close();
}
}
}
private int InsertImage(int ImageID, byte[] Image)
{
SqlCommand InsertSqlCommand = new SqlCommand();
int RetunId = 0;
SqlCommand com = new SqlCommand();
SqlConnection con = new SqlConnection();
con = GetConnectObj();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "SaveImage";
SqlParameter objOutputParam = new SqlParameter("@NewID", SqlDbType.Int);
objOutputParam.Direction = ParameterDirection.Output;
SqlParameter objImageParam = new SqlParameter("@ImageFile", SqlDbType.Image);
objImageParam.Direction = ParameterDirection.Input;
objImageParam.Value = Image;
SqlParameter objImageIDParam = new SqlParameter("@ImageID", SqlDbType.Int, 12, ParameterDirection.Input);
objImageIDParam.Value = ImageID;
com.Parameters.Add(objImageIDParam);
com.Parameters.Add(objImageParam);
com.Parameters.Add(objOutputParam);
con.Open();
com.ExecuteNonQuery();
RetunId = objOutputParam.Value;
return RetunId;
}
public DataTable ReturnImage(int ImageIdvalue)
{
dtReturnImage = FillDatatable(" select ImageFile from Images where ImageID = " + ImageIdvalue + "");
return dtReturnImage;
}
private string ConnectionString()
{
StrConn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Santosh_Practice;Data Source=SANTOSH\\SQLEXPRESS"; //Myconnection is Key in App.Config
return StrConn;
}
private SqlConnection GetConnectObj()
{
return new SqlConnection(ConnectionString());
}
private DataTable FillDatatable(string Query)
{
DataTable objDT = new DataTable();
SqlDataAdapter objda = new SqlDataAdapter(Query, ConnectionString());
objda.Fill(objDT);
return objDT;
}

Regards
Santosh

Comments or Responses

Login to post response