Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store image in database in linq to sql [closed]

Can any one guide which will be the best way to store images to the database? I am using Sql server 2008. Currently I am storing file path in database but when I am trying to fetch it, it is not fetched.

Here is code to save image:

string Extention = System.IO.Path.GetExtension(fuBanner.FileName);
if (Extention == ".jpg" || Extention == ".jpeg" || Extention == ".bmp" || Extention == ".PNG")
 {
    result.Banner_SignUp_Page = System.IO.Path.GetExtension(fuBanner.FileName);
    db.SubmitChanges();
    lblMsg.Visible = true;
    Response.Redirect("ListOfEvent.aspx");
    //lblMsg.Text = "Records updated successfully.";
 }

And code for fetching the image:

var result1 = from a in db.EMR_INVITATIONs
    join b in db.EMR_EVENTs on a.EventID equals b.EventID
    where b.EventID == (int)Session["eventid"]   
        select new
                 {
                    Banner = b.Banner_SignUp_Page,
                    Title = b.Title  
                 };
like image 417
Monika Avatar asked Jan 30 '26 08:01

Monika


2 Answers

In your case:

Below is possible code for what you want to achieve:

protected void btnUpload_Click(object sender, EventArgs e)
{     
    if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0)
    {
        string path_file_name = fuFileUploader.PostedFile.FileName;
        string ext = Path.GetExtension(path_file_name).Replace(".", "");
        string file_name = Path.GetFileNameWithoutExtension(path_file_name);
        string file_title = txtFileTitle.Text.Trim();
        HelperDataClassesDataContext db = new HelperDataClassesDataContext();

        try
        {
            byte[] file_byte = fuFileUploader.FileBytes;
            Linq.Binary file_binary = new Linq.Binary(file_byte);

            ControlDocument cd = new ControlDocument
            {
                guid = Guid.NewGuid(),
                file_ext = ext,
                file_nm = file_name.Trim(),
                file_title=file_title,
                file_bin = file_binary,
                is_active = true,
                upload_page_type = rblLocation.SelectedValue,
                upload_dt = DateTime.Now,
                upload_by = UtilUniverse.Common.CurrentUserLogin.Trim()
            };

            db.ControlDocuments.InsertOnSubmit(cd);
        }
        finally
        {

            db.SubmitChanges();
        }
    }

}
like image 57
C Sharper Avatar answered Feb 01 '26 20:02

C Sharper


A code to resize before saving image to disk. It save path in database only not image in database. If we save path in database and files on disk, then we can save CPU cost each time when we have to display that image , (give it a try). You can use it for saving any image of your project, just provide the uploader control and image path to it.

public static bool SaveImage(HttpPostedFile imageUpload, string imagePath, out string error)
{
    error = "";
    string extension = Path.GetExtension(imageUpload.FileName);
    if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
    {
        try
        {
            System.Drawing.Bitmap objImage = new System.Drawing.Bitmap(imageUpload.InputStream);
            System.Drawing.Bitmap outImage = new Bitmap(170, 200);
            System.Drawing.Graphics outGraphics = Graphics.FromImage(outImage);
            SolidBrush sb = new SolidBrush(System.Drawing.Color.White);
            outGraphics.FillRectangle(sb, 0, 0, 170, 200);
            outGraphics.DrawImage(objImage, 0, 0, 170, 200);
            sb.Dispose();
            outGraphics.Dispose();
            objImage.Dispose();
            outImage.Save(System.Web.HttpContext.Current.Server.MapPath(imagePath));
            return true;
        }
        catch (Exception e)
        {
            error = "Failed to save Image. Error: " + e.Message;
        }
    }
    else
    {
        error = "Invalid image foramt.";
    }
    return false;
}

then you can simply fetch it by getting image path.
i.e.
image.url = //image column value

like image 45
Sohail Avatar answered Feb 01 '26 22:02

Sohail