0

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

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

4

1 回答 1

0

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

在.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
        }
}
于 2013-03-01T10:52:38.827 回答