2011-03-02 166 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