您需要将图像转换为字节以将其存储在数据库中。您可以IO.File.ReadAllBytes()
用于本地文件或Net.WebClient.DownloadData()
Web 文件。该IMAGE
数据类型在 SQL Server 中将消失,因此请用于VARBINARY(MAX)
存储它。
代码:
Dim url As String = "http://example.com/image.png",
file As String = "c:\image.png"
Using connection As New Data.SqlClient.SqlConnection("your connection string"),
saveImage As New Data.SqlClient.SqlCommand("dbo.saveImage", connection)
connection.Open()
saveImage.CommandType = Data.CommandType.StoredProcedure
'use only one of these
saveImage.Parameters.AddWithValue("@imageData", New Net.WebClient().DownloadData(url)) 'get file from url
'saveImage.Parameters.AddWithValue("@imageData", IO.File.ReadAllBytes(file)) 'get file locally
saveImage.ExecuteNonQuery()
End Using
程序:
CREATE PROCEDURE [dbo].[saveImage] ( @imageData AS VARBINARY(MAX) )
AS
INSERT INTO dbo.myImage ( imageData ) VALUES ( @imageData )
桌子:
CREATE TABLE [dbo].[myImage](
[imageData] [varbinary](max) NOT NULL
)