having problem with inserting an image into a database
if (uploadimg.PostedFile != null && uploadimg.PostedFile.FileName != "")
{
string img_id = "1";
byte[] myimage = new byte[uploadimg.PostedFile.ContentLength];
HttpPostedFile Image = 开发者_C百科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());
}
}
what changes should i make in my c# code so that i can avoid my sql exception of primary key constrain as i hav used my trigger
create trigger trig_image on ImageGallery After insert as begin declare @id int; set @id=1; update ImageGallery set img_id='img'+cast(@id as varchar(10)) set @id=@id+1; end go
and columns are img_id varchar(50),image_size bigint,image_content image
By default, SQL Server will try to convert 'img' to an integer to add @id
to it. Change:
set img_id = 'img' + @id
to
set img_id = 'img' + cast(@id as varchar(12))
Your trigger is very dangerous..... the code as you have it will update all rows in your table ImageGallery
everytime an insert happens - is that really what you want??
Also - to have a separate column with img001, img002
etc., I would use a computed column:
ALTER TABLE dbo.ImageGallery
ADD ImageText AS 'img' + CAST(img_id AS VARCHAR(5)) PERSISTED
With this, you get a new column called ImageText
which will hold values img1
, img2
and so forth - automatically, without any need for a messy trigger or anything - it just works!
The problem is in your Trigger statement.
You have to explicitly convert the @id
variable to a varchar.
declare @id int;
set @id=1;
update ImageGallery
set img_id='img'+ convert(varchar(10), @id)
set @id=@id+1
EDIT:
You are probably getting a PK constraint violation because:
- You are trying to update all
img_id
's rather than the one you have just inserted - You are always setting the
@id
variable to 1 so each time the trigger runs the@Id
value will always be 1
Another option would be to change your table structure and have img_id
as an integer identity column. This will save you from having to manually calculate the ID as SQL Server will automatically increment the value on each insert.
You can easily add the 'img' prefix before displaying to the client.
e.g
example select
Select 'img' + convert(varchar(10), img_id) as 'Formatted_Img_id'
from ImageGallery
example update
Update ImageGallery
Set Description = 'blah blah test'
Where img_id = Replace(img_id, 'img','')
What is the date type of the field image_id?
I assume it's of type INT. I recommend using date type varchar.
精彩评论