试图将文本和图像都插入数据库。唯一的必填字段是类别、名称和描述。如果只有这些字段有条目,则插入到数据库中是不成功的。如果加载了不需要的 RecipePicture,则插入成功。我正在使用 SqlServer。
问题必须在后面的代码中,但我找不到它。任何帮助将不胜感激,因为我需要能够在不插入图片/图像的情况下插入数据库。后面的代码:
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null
&& FileUpload1.PostedFile.FileName != "")
{
byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AsianConnectionString"].ConnectionString);
SqlCommand storeimage = new SqlCommand("INSERT INTO AsianRecipe"
+ "(Category, Name, Description, RecipePicture, RecipePictureType, RecipePictureSize, UserName, UserPicture, UserPictureType, UserPictureSize) "
+ " values (@Category, @Name, @Description, @image, @imagetype, @imagesize, @UserName, @userpicture, @userpicturetype, @userpicturesize)", myConnection);
storeimage.Parameters.Add("@image", SqlDbType.VarBinary, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value
= FileUpload1.PostedFile.ContentType;
storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value
= FileUpload1.PostedFile.ContentLength;
storeimage.Parameters.Add("@category", SqlDbType.NVarChar, 50).Value
= lblSelection.Text;
storeimage.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value
= TextBox2.Text;
storeimage.Parameters.Add("@description", SqlDbType.NVarChar, 250).Value
= TextBox3.Text;
storeimage.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value
= TextBox4.Text;
storeimage.Parameters.Add("@userpicture", SqlDbType.VarBinary, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@userpicturetype", SqlDbType.VarChar, 100).Value
= FileUpload2.PostedFile.ContentType;
storeimage.Parameters.Add("@userpicturesize", SqlDbType.BigInt, 99999).Value
= FileUpload2.PostedFile.ContentLength;
myConnection.Open();
storeimage.ExecuteNonQuery();
myConnection.Close();
}
}
protected void OnSelect(object sender, EventArgs e)
{
lblSelection.Text = ((LinkButton)sender).Text;
}
}
}