我使用longblob数据类型来插入图像,但它会出错。
我想将图片框中的图片插入到mysql数据库中。
答案 0 :(得分:0)
我这里有一个c#实现,但您可以使用a c# to VB.Net converter
在aspx文件中:
<asp:FileUpload runat="server" ID="fuPersonPicture" />
<asp:Button runat="server" ID="btnPhotoUpload" Text="Upload" OnClick="btnPhotoUpload_Click" />
在代码隐藏中:
protected void btnPhotoUpload_Click(object sender, EventArgs e)
{
byte[] rawData = new byte[fuPersonPicture.FileBytes.Length];
int fileSize = fuPersonPicture.FileBytes.Length;
fuPersonPicture.FileContent.Read(rawData, 0, fileSize);
string extension = Path.GetExtension(fuPersonPicture.PostedFile.FileName);
switch (extension.ToLower())
{
// Only allow uploads that look like images.
case ".jpg":
case ".jpeg":
case ".gif":
case ".png":
case ".bmp":
System.Drawing.Image myImage = System.Drawing.Image.FromStream(fuPersonPicture.PostedFile.InputStream);
fuPersonPicture.FileContent.Close();
PersonPictureManager.Insert(fuPersonPicture.FileName, fileSize, rawData, myImage.Height, myImage.Width);
//lbUploadMessage.Text = string.Empty;
break;
default:
//lbUploadMessage.Text = "<br>File is not a valid image file (JPG, JPEG, GIF, BMP, PNG).";
break;
}
fuPersonPicture.FileContent.Close();
}
PersonPictureManager.Insert()
public static void Insert(string FileName, int FileSize, byte[] RawData, int ImageHeight, int ImageWidth)
{
string strSQL = "insert into person_picture (file_id, file_name, file_size, file, width, height) values (NULL, @file_name, @file_size, ?file, @width, @height)";
using (MySqlCommand cmd = new MySqlCommand(strSQL))
{
cmd.Parameters.Add(new MySqlParameter("@file_name", FileName));
cmd.Parameters.Add(new MySqlParameter("@file_size", FileSize));
cmd.Parameters.Add(new MySqlParameter("@height", ImageHeight));
cmd.Parameters.Add(new MySqlParameter("@width", ImageWidth));
cmd.Parameters.Add(new MySqlParameter("?file", RawData));
DatabaseConnection.ExecuteNonQuery(cmd); // Just a DB helper class
}
}