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