Store Photo in Sql Server



byte[] ReadImageToBytes(String sPath)   ///This Metod Conver the Photo IN Binary
{
    byte[] data = null;
    FileInfo finfo = new FileInfo(sPath);
    long numBytes = 0;
    numBytes = finfo.Length;
    FileStream fstream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fstream);
    data = br.ReadBytes((int)numBytes);
    fstream.Close();
    return data;
}
private void SaveImage(string comstr, string ImagePath)
{
    try
    {
        byte[] imageSampleData = null;
        SqlCommand com = new SqlCommand(comstr, con);
        imageSampleData = ReadImageToBytes(ImagePath); //From Here The Method For Conver Image To Byte Can Be Call
        com.Parameters.Add(new SqlParameter("@Image", (object)imageSampleData));//Here Image Can Be Stored Into Database
        com.ExecuteNonQuery();
        com.Parameters.Clear();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

No comments: