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圖像
您正在使用SQL Server的**版本**?如果你在2005或更新:**不要**使用數據類型'IMAGE'了!它已經死了 - 用'VARBINARY(MAX)'代替。 – 2011-03-02 08:50:05
所以它會工作使用varbinary(最大),因爲它以前的工作,它給我一個錯誤在img_id而不是在image_content ..我應該在我的c#代碼 – shweta 2011-03-02 08:52:28