2013-12-13 38 views
3

任何一個指南都可以將圖像存儲到數據庫中嗎?我正在使用Sql server 2008.目前我正在數據庫中存儲文件路徑,但是當我試圖獲取它時,它不會被抓取。在LINQ to sql中存儲數據庫圖像的最佳方式

下面是代碼來保存圖像:

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."; 
} 

和代碼獲取圖像:

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 
       }; 
+1

它沒有被提取意味着?你可以發佈代碼嗎? –

+0

好的,請稍候im發帖代碼 – Monika

+1

也看看是否http://debugmode.net/2010/05/10/inserting-and-retrieving-image-using-linq-to-sql-from-asp-net-application/ this鏈接有幫助 –

回答

4

你的情況:

下面是可能的代碼,你想達到什麼:

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(); 
     } 
    } 

} 
1
InstituteRegistration insti = new InstituteRegistration(); 
if (FileUploadInstituteImage.PostedFile.ContentLength != 0) 
     { 
      string extension =   System.IO.Path.GetExtension(FileUploadInstituteImage.FileName); 
      if (extention.ToLower() == ".jpg" || extention.ToLower() == ".jpeg" || extention.ToLower() == ".gif" || extention.ToLower() == ".png" || extention.ToLower() == ".bmp" || extention.ToLower() == ".tif" || extention.ToLower() == ".tiff") 
      { 
       Stream fsInstitute = FileUploadInstituteImage.PostedFile.InputStream; 
       BinaryReader brInsti = new BinaryReader(fsInstitute); 
       Byte[] bytesInstitute = brInsti.ReadBytes((Int32)fsInstitute.Length); 
       insti.InstituteImage = bytesInstitute; 
      } 
      else 
      { 
       lblMsg.Text = "Invalid File."; 
       return; 
      } 
     } 

db.InstituteRegistrations.InsertOnSubmit(insti); 
db.SubmitChanges(); 
4

將圖像保存到磁盤之前調整大小的代碼。它在數據庫中保存路徑而不是數據庫中的映像。如果我們將數據庫和文件的路徑保存在磁盤上,那麼當我們必須顯示該圖像時(每次嘗試),我們都可以節省CPU成本()。您可以使用它來保存項目的任何圖像,只需提供上傳控件和圖像路徑即可。

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; 
} 

然後你可以簡單地通過獲取圖像路徑來獲取它。

image.url = //圖像列值

+0

感謝您的回覆。 – Monika

+0

這不會保存圖像作爲用戶想要的數據庫 –

+0

是的,它不保存圖像在數據庫中,但它保存圖像在磁盤和數據庫的路徑,雖然用戶不需要它,但通過這個用戶可能會考慮保存的好處數據庫中的路徑和磁盤上的映像。 – Sohail

相關問題