2014-02-11 69 views
0

我有代號的一部分,其插入DB SQL SERVER的圖像更新,但是當我再次嘗試重新插入我有,關鍵是重複的,所以我在尋找一個錯誤一種方式插入圖像,如果它的存在,它會更新一個我,這是我的代碼在數據庫中插入:插入圖像,如果它存在

if (ImageUpload.HasFile && ImageUpload.PostedFile.ContentLength > 0) 
      { 
    string filename = ImageUpload.FileName; 
        byte[] filebyte = ImageUpload.FileBytes; 
        Binary binaryObj = new Binary(filebyte); 
        DataClasses1DataContext context = new DataClasses1DataContext(); 
        context.ImageStorage.InsertOnSubmit(
         new ImageStorage 
          { 
           PPR = Convert.ToInt32(Session["Code"]), 
           ImageContentType = ImageUpload.PostedFile.ContentType, 
           ImageName = filename, 
           ImageBinary = binaryObj 
          }); 

        context.SubmitChanges(); 
        this.LabelInfo.ForeColor = Color.Green; 

        this.LabelInfo.Text = "Upload ok."; 
       } 
       catch(Exception xcp) 
       { 
        this.LabelInfo.Text = xcp.Message; 

        this.LabelInfo.ForeColor = Color.Red; 
       } 

      } 

編輯:

這是我的表(圖片)列:

imgid:PK詮釋PPR F:ķINT圖像:VARBINARY圖片二進制:圖片名稱:VARCHAR

+0

是什麼ImageStorage表的架構和列是主鍵? –

+0

這個'C'如何? – Sadique

+0

我想插入或更新圖像(如果存在)根據其他表中的列這個這個colomn是「PPR」,我用這種方式:PPR = session [「Code」] – Meryem

回答

0

要實現的功能所適用如下算法:

  1. 查詢的ImageStorage表鍵值。
  2. 如果步驟1的輸出不爲空,則設置步長爲1的新binaryObj
  3. 否則返回如果第1步返回null,然後做InsertOnSubmit因爲你正在做的目前ImageStorage對象ImageBinary列。
相關問題