我有一部分代码在DB SQL SERVER中插入图像,但是当我再次尝试重新插入时,我有一个错误,即密钥是重复的,所以我正在寻找一种插入图像的方法,如果它存在它将更新我拥有的那个,这是我在DB中插入的代码:
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 Int ppr F:K int image:varbinary ImageBinary:image Name:varchar
答案 0 :(得分:0)
要实现该功能,请应用以下算法:
ImageStorage
表。ImageBinary
ImageStorage
对象的binaryObj
列
InsertOnSubmit
。