本文介绍了如何在 MySQL 数据库中插入图像并使用 vb.net 检索它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 longblob 数据类型插入图像,但它给出了错误.

I am using the longblob data type to insert image but it gives error.

我想从图片框中插入图片到mysql数据库.

I want to insert image from picture box to mysql database.

推荐答案

我这里有 ac# 实现,但你可以使用 ac# 到 VB.Net 转换器

I have here a c# implementation but you can use a c# to VB.Net converter

在 aspx 文件中:

In the aspx file:

<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
        }
}

这篇关于如何在 MySQL 数据库中插入图像并使用 vb.net 检索它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:13