vb.net 如何将图像插入到 SQL Server 数据库表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29439438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:57:12  来源:igfitidea点击:

How to insert images into SQL Server database table

sql-servervb.netdatagridview

提问by arthur

I have created a new SQL Server local database with a table called drink. I use Microsoft Visual Studio 2008.

我创建了一个新的 SQL Server 本地数据库,其中包含一个名为drink. 我使用 Microsoft Visual Studio 2008。

Inside the table I defined the following columns:

在表中,我定义了以下列:

id [int], kind [varchar], year [datatime], image [image]

I would like to insert images into the imagecolumn but I don't know how to do.

我想在image列中插入图像,但我不知道该怎么做。

I need this column, because I want to display all data in DataGridView using VB.NET

我需要此列,因为我想使用 VB.NET 在 DataGridView 中显示所有数据

Thanks for help!

感谢帮助!

采纳答案by bri

This worked for me...

这对我有用...

Imports System.Data.Sql
Imports System.IO
Imports System.drawing

Module Module1

    Private Const _ConnectString As String = "your connection string here"

     Sub Main()
        Dim MyImage As Image = Image.FromFile("RandomImage.jpg")
        Dim Id As Long = 1
        SaveDrinkImage(MyImage, Id)
     End Sub

    Sub SaveDrinkImage(MyImage As Image, Id As Long)

        Dim ImageBytes(0) As Byte
        Using mStream As New MemoryStream()
                 MyImage.Save(mStream, MyImage.RawFormat)
                 ImageBytes = mStream.ToArray()
        End Using

        Dim adoConnect = New SqlClient.SqlConnection(_ConnectString)
        Dim adoCommand = New SqlClient.SqlCommand("UPDATE [drink] SET [image]=@MyNewImage WHERE [id]=@id", adoConnect)

        With adoCommand.Parameters.Add("@MyNewImage", SqlDbType.Image)
            .Value = ImageBytes
            .Size = ImageBytes.Length
        End With
        With adoCommand.Parameters.Add("@id", SqlDbType.BigInt)
            .Value = Id
        End With

        adoConnect.Open()
        adoCommand.ExecuteNonQuery()
        adoConnect.close()

    End Sub

End Module

回答by knkarthick24

Using openrowset you can insert image into database:

使用 openrowset 您可以将图像插入数据库:

insert into tableName (id,kind,ImageColumn) 
SELECT 1,'JPEG',BulkColumn 
FROM Openrowset( Bulk '<Path of the image>', Single_Blob) as img

回答by Craig Johnson

Try this:

尝试这个:

Sub SaveDrinkToDB(name As String, imageFilePath As String)
    Using cn = New SqlConnection("connection string here")
        cn.Open()
        Using cmd = New SqlCommand("INSERT INTO Drink (Name, Image) Values(@Name,@Image)", cn)
            cmd.Parameters.AddWithValue("@name", name)
            cmd.Parameters.AddWithValue("@Image", File.ReadAllBytes(imageFilePath))
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Usage:

用法:

    SaveDrinkToDB("Milk", "c:\drink.png")

回答by tushar mathur

USE [database_name]
GO
INSERT INTO [schema].[tablename]([property1],[property2],[Property3],[Image])
     VALUES
           ('value','value','value','C:\pathname.jpg')
GO

回答by Infojam

There are two approaches. You can store the image as a blob in the database or you can store the path (string) to the image file. There is an answer that discusses this: Storing images in SQL Server?

有两种方法。您可以将图像作为 blob 存储在数据库中,也可以将路径(字符串)存储到图像文件中。有一个讨论这个的答案:Storing images in SQL Server?