如何将图像插入 SQL 服务器数据库 table
How to insert images into SQL Server database table
我创建了一个新的 SQL 服务器本地数据库,其中包含一个名为 drink
的 table。
我使用微软 Visual Studio 2008.
在 table 中我定义了以下列:
id [int], kind [varchar], year [datatime], image [image]
我想在 image
列中插入图片,但我不知道该怎么做。
我需要此列,因为我想使用 VB.NET
在 DataGridView 中显示所有数据
感谢帮助!
使用 openrowset 可以将图像插入数据库:
insert into tableName (id,kind,ImageColumn)
SELECT 1,'JPEG',BulkColumn
FROM Openrowset( Bulk '<Path of the image>', Single_Blob) as img
有两种方法。您可以将图像作为 blob 存储在数据库中,也可以将路径(字符串)存储到图像文件。有一个讨论这个的答案:Storing images in SQL Server?
这对我有用...
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
试试这个:
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
用法:
SaveDrinkToDB("Milk", "c:\drink.png")
USE [database_name]
GO
INSERT INTO [schema].[tablename]([property1],[property2],[Property3],[Image])
VALUES
('value','value','value','C:\pathname.jpg')
GO
我创建了一个新的 SQL 服务器本地数据库,其中包含一个名为 drink
的 table。
我使用微软 Visual Studio 2008.
在 table 中我定义了以下列:
id [int], kind [varchar], year [datatime], image [image]
我想在 image
列中插入图片,但我不知道该怎么做。
我需要此列,因为我想使用 VB.NET
在 DataGridView 中显示所有数据感谢帮助!
使用 openrowset 可以将图像插入数据库:
insert into tableName (id,kind,ImageColumn)
SELECT 1,'JPEG',BulkColumn
FROM Openrowset( Bulk '<Path of the image>', Single_Blob) as img
有两种方法。您可以将图像作为 blob 存储在数据库中,也可以将路径(字符串)存储到图像文件。有一个讨论这个的答案:Storing images in SQL Server?
这对我有用...
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
试试这个:
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
用法:
SaveDrinkToDB("Milk", "c:\drink.png")
USE [database_name]
GO
INSERT INTO [schema].[tablename]([property1],[property2],[Property3],[Image])
VALUES
('value','value','value','C:\pathname.jpg')
GO