I am using the longblob data type to insert image but it gives error.
我使用longblob数据类型来插入图像,但它给出了错误。
I want to insert image from picture box to mysql database.
我想从图片框插入图像到mysql数据库。
1 个解决方案
#1
0
I have here a c# implementation but you can use a c# to VB.Net converter
我这里有一个c#实现,但你可以使用c#到VB.Net转换器
In the aspx file:
在aspx文件中:
<asp:FileUpload runat="server" ID="fuPersonPicture" />
<asp:Button runat="server" ID="btnPhotoUpload" Text="Upload" OnClick="btnPhotoUpload_Click" />
In code-behind:
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();
}
Definition of PersonPictureManager.Insert()
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
}
}
#1
0
I have here a c# implementation but you can use a c# to VB.Net converter
我这里有一个c#实现,但你可以使用c#到VB.Net转换器
In the aspx file:
在aspx文件中:
<asp:FileUpload runat="server" ID="fuPersonPicture" />
<asp:Button runat="server" ID="btnPhotoUpload" Text="Upload" OnClick="btnPhotoUpload_Click" />
In code-behind:
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();
}
Definition of PersonPictureManager.Insert()
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
}
}