将多个图像添加到SQL列 [英] Adding multiple images to SQL column

查看:89
本文介绍了将多个图像添加到SQL列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图将多个选定的图像添加到我的sql数据库中。问题是当我选择我的图像文件并提交保存时,只有1个被保存到数据库中。

这里是代码:



SO I have tried to add multiple selected images to my sql database. The problem is when I select my image files and commit to saving, only 1 gets saved to the database.
here is the code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim Files As New OpenFileDialog
    Files.Filter = "Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"
    ' Allow the user to select multiple images.
    Files.Multiselect = True
    Files.Title = "Select an image"
    Files.ShowDialog()
    'Image.FromFile(Files.FileName)
    'PictureBox1.Image = Image.FromFile(Files.FileName)

    Dim sqlcon As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\jj\documents\visual studio 2017\Projects\sqlpictureloader\sqlpictureloader\Pictures.mdf;Integrated Security=True")
    Dim sqladapt = New SqlDataAdapter("Select * from [Table]", sqlcon)

    sqlcon.Open()
    Dim cmd As SqlClient.SqlCommand
    Dim sql As String = "insert into [Table] values(@images)"
    cmd = New SqlClient.SqlCommand(sql, sqlcon)

    Using ms As MemoryStream = New MemoryStream()

        Dim bm As Bitmap = New Bitmap(Image.FromFile(Files.FileName))
        bm.Save(ms, Image.FromFile(Files.FileName).RawFormat)

        Dim arrPic() As Byte = ms.GetBuffer()

        cmd.Parameters.AddWithValue("@images", arrPic)

    End Using

    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    cmd.ExecuteNonQuery()

    sqlcon.Close()

    MessageBox.Show("New Records Added")

    Dim dt As New DataTable
    Dim ds As New DataSet
    sqladapt.Fill(dt)
    DataGridView1.DataSource = dt


End Sub





我很感激帮助。



什么我试过了:



我试图为位图文件的每个语句做一个,但我似乎无法做到或者。



I appreciate the help.

What I have tried:

I have tried to do a for each statement for the files that are bitmap, but I can't seem to get that to work either.

推荐答案

如果我是你,我会重新考虑在db中存储图像。我会将图像存储在文件系统上,然后将路径存储到数据库中的图像。



如果您仍打算将数据存储在数据库中,请查看本文。



如何使用asp.net从SQL Server数据库存储和检索图像?文章和例子。 [ ^ ]
If I were you I would rethink storing an image in the db.I would store the image on the file system and then store the path to the image in the db.

If you still intend to store the images in your db have a look at this article.

How to store and retrieve images from SQL server database using asp.net? article and examples.[^]


这篇关于将多个图像添加到SQL列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆