如何动态上传图像到SQL Server数据库? [英] How do I dynamically upload images to SQL server database?

查看:73
本文介绍了如何动态上传图像到SQL Server数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码用于制作列并向datagridview添加图像,但现在我正在将这些图像保存到数据库中。



我是什么要知道是必须添加图像列,所以我有这部分代码:

The code I have works for making columns and adding images to the datagridview, but now I am moving on towards saving these images into a database.

What I do know is that columns for images must be added, so I have this portion of code:

Try
            Dim sqlcon As New SqlConnection("mysqlstatement")
            Dim sqladapt = New SqlDataAdapter("Select * from [Table]", sqlcon)

            sqlcon.Open()
            Dim cmd As SqlClient.SqlCommand
            Dim sql As String = "ALTER TABLE [table] ADD IMG image"
            cmd = New SqlClient.SqlCommand(sql, sqlcon)

            cmd.ExecuteNonQuery()
            sqlcon.Close()

            MessageBox.Show(sql.ToString)
        Catch ex As Exception


        End Try





然后我需要把它与我已经拥有的东西联系起来:



then I need to tie this into what I already have which is:

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

<pre>ofd.Multiselect = True
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim fileCount = ofd.FileNames.Length
            Dim images(fileCount - 1) As Image


            For i As Integer = 0 To fileCount - 1
                If i >= DataGridView1.Columns.Count Then
                    Dim col As New DataGridViewImageColumn()

                    DataGridView1.Columns.Add(col)


                End If

                'adds images to panel control
                images(i) = Image.FromFile(ofd.FileNames(i))


            Next

            DataGridView1.Rows.Add(images)


        End If





问题是我不确定如何使用sql语句动态创建多个列。在动态sql列语句之后,那么如何在不进行另一个sql语句的情况下同时输入图像呢?在正确的指导下,我可以从那里学到新的东西并从那里扩展。



我尝试过:



尝试将用



The problem is I am unsure how to make multiple columns dynamically with an sql statement. After the dynamic sql column statement, then how do I input images at the same time, without making another sql statement? Figured with the right guidance I could learn something new and expand from there.

What I have tried:

Tried to tie in the columns created with the

Dim col As New DataGridViewImageColumn()

并试图将其串起来,但是我我假设它不起作用的原因是因为列名相同而且SQL不喜欢它。



我还考虑过为每个语句使用a对于上传到gridview的图片,但不确定如何在sql语句的组合中编写它。



所以当涉及到这个复杂的东西时,我有点迷失,或者至少对我来说是复杂的。

and tried to string that, but I am assuming the reason that it wasn't working was because the columns were the same name and SQL does not like that.

I also thought about using a for each statement for the pictures being uploaded into the gridview, but was unsure how to write that in combination of the sql statement.

So I am a little lost when it comes to something this complex, or at least to me its complex.

推荐答案

甚至不尝试修改表格:这是错误的方法。

我假设您正在尝试添加列,因为您有与特定行相关的未知数量的图像,并认为为每个添加一列将做你想要的。它不会。添加列时,将其添加到表中的每一行 - 因此,如果您有一行需要20个图像的空间,那么每行都会获得该空间,即使它们只有一个图像。



相反,表格中没有任何行可以存储图像!

让我们假设这些是产品图片,并且产品可以有不同数量的图片 - 就像在eBay或亚马逊上出售的东西一样。

你有你的产品table:

Don't even try to modify the table: that's the wrong approach.
I assume you are trying to add columns because you have an unknown number of images related to a specific row, and think that adding a column for each will do what you want. It won't. When you add a column, you add it to every row in the table - so if you have one row which needs space for 20 images then every row gets that space, even if they have only one image.

Instead, don't have any rows in the table for storing images at all!
Lets assume these are product pictures, and that a product can have a variable number of images - just as something for sale on eBay or Amazon might.
You have your Products table:
ID              INT, IDENTITY (or UNIQUEIDENTIFIER, depending on your preference)
Title           NVARCHAR(255)
Description     NVARCHAR(MAX)
Price           DECIMAL(8,2)
Stock           INT

但不是添加IMAGE列,而是创建第二个表图像:

But instead of adding IMAGE columns, you create a second table Images:

ID              INT, IDENTITY (Or UNIQUEIDENTIFIER)
ProductID       Same as ID in the Products table, FOREIGN KEY to Products.ID
ImageData       IMAGE

然后您可以添加任意数量的图像因为您需要产品,并在需要时使用产品ID检索它们。

You can then add as many images as you need to a Product, and retrieve them using the Product ID when you need them.


这篇关于如何动态上传图像到SQL Server数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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