2011-03-02 98 views
0
if (uploadimg.PostedFile != null && uploadimg.PostedFile.FileName != "") 
    { 

     string img_id = "1"; 
     byte[] myimage = new byte[uploadimg.PostedFile.ContentLength]; 
     HttpPostedFile Image = uploadimg.PostedFile; 
     Image.InputStream.Read(myimage, 0, (int)uploadimg.PostedFile.ContentLength); 

     SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true"); 
     SqlCommand storeimage = new SqlCommand("INSERT INTO ImageGallery " + "(img_id,image_Size,image_Content) " + " values (@img_id,@imagesize,@image)", myConnection); 
     storeimage.Parameters.AddWithValue("@img_id",img_id); 
     // storeimage.Parameters.Add("@id",SqlDbType.VarChar,100) = uploadimg.PostedFile.ContentType; 
     //storeimage.Parameters.Add('@id',id); 
     storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = uploadimg.PostedFile.ContentLength; 
     storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage; 



     try 
     { 
      myConnection.Open(); 
      storeimage.ExecuteNonQuery(); 
      myConnection.Close(); 

     } 
     catch (Exception e) 
     { 
      Response.Write(e.ToString()); 
     } 
    } 

我應該讓我的C#代碼什麼樣的變化,使我可以避開主鍵的我的SQL異常壓抑,因爲我甲肝用我的觸發與插入圖像到數據庫

創建觸發器trig_image on ImageGallery 插入 之後開始 declare @id int; set @ id = 1; update ImageGallery set img_id ='img'+ cast(@id as varchar(10)) set @ id = @ id + 1; 結束 去

和列img_id VARCHAR(50),IMAGE_SIZE BIGINT,image_content圖像

+1

您正在使用SQL Server的**版本**?如果你在2005或更新:**不要**使用數據類型'IMAGE'了!它已經死了 - 用'VARBINARY(MAX)'代替。 – 2011-03-02 08:50:05

+0

所以它會工作使用varbinary(最大),因爲它以前的工作,它給我一個錯誤在img_id而不是在image_content ..我應該在我的c#代碼 – shweta 2011-03-02 08:52:28

回答

0

什麼是場image_id的日期類型?

我認爲它是INT類型的。我建議使用日期類型varchar。

+0

在我意義上使用SQL Server 2005中進行什麼樣的更改...和image_content數據類型是圖像...它工作之前,當我使用它簡單..但是當我特別使用img_id它不是 – shweta 2011-03-02 08:50:26

+0

@beacon是我使用varchar – shweta 2011-03-02 08:54:24

+0

嘿thanxxx..i得到圖像ka id .. – shweta 2011-03-02 08:59:54

1

默認情況下,SQL Server將嘗試將'img'轉換爲整數,以便將@id添加到它。變化:

set img_id = 'img' + @id 

set img_id = 'img' + cast(@id as varchar(12)) 
+0

燁工作後,我轉換... thnxx – shweta 2011-03-02 09:02:20

+0

請解決我的問題....我已更新! – shweta 2011-03-02 09:49:54

+0

@shweta:看到marc_s'回答 – Andomar 2011-03-02 10:54:09

1

的問題是在你的觸發聲明。

您必須明確地將@id變量轉換爲varchar。

declare @id int; 
set @id=1; 

update ImageGallery 
set img_id='img'+ convert(varchar(10), @id) 

set @[email protected]+1 

編輯:

你可能會得到一個PK約束衝突的原因是:

  • 您試圖更新所有img_id的,而不是你剛剛插入
  • 你一個總是將@id變量設置爲1,因此每次觸發器運行時@Id的值將始終爲1

另一種選擇是更改表格結構並將img_id設置爲integer identity column。這將使您不必手動計算ID,因爲SQL Server會自動增加每個插入的值。

您可以在顯示給客戶端之前輕鬆添加'img'前綴。

e.g

例如選擇

Select 'img' + convert(varchar(10), img_id) as 'Formatted_Img_id' 
from ImageGallery 

例如更新

Update ImageGallery 
Set Description = 'blah blah test' 
Where img_id = Replace(img_id, 'img','') 
+0

是的它的工作..thnxx很多..但現在我沒有得到馬image_content – shweta 2011-03-02 09:02:48

+0

現在它給了我一個錯誤sayin我違反了我的主鍵約束... – shweta 2011-03-02 09:09:49

+0

@shweta - 我會這是因爲1)你試圖更新表中的所有Img_id而不是隻更新新行。 2)您將@id設置爲1,所以每次觸發器運行時都會嘗試將img_id更新爲1. – codingbadger 2011-03-02 09:11:42

1

,您的觸發是非常危險的.....代碼,因爲你有它將更新所有行在您的表ImageGallery每次插入發生 - 是你真正想要什麼?

而且 - 有一個單獨的列與img001, img002等,我會用一個計算列

ALTER TABLE dbo.ImageGallery 
    ADD ImageText AS 'img' + CAST(img_id AS VARCHAR(5)) PERSISTED 

有了這個,你會得到一個名爲ImageText新列將保存值img1img2和等等 - 自動,而不需要任何雜亂的觸發器或任何東西 - 它只是工作!

+0

你在問題中寫的是什麼,我想...現在我得到了答案menas我得到了我的image_id,但我的圖像內容不是現在來的 – shweta 2011-03-02 09:01:30

+0

@shweta:你爲什麼要自己設置「img_id」?只需將其設置爲「INT IDENTITY」字段並讓數據庫處理編號即可...... – 2011-03-02 10:59:04