如何将图像插入到SQL Server数据库表中 [英] How to insert images into SQL Server database table
本文介绍了如何将图像插入到SQL Server数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个新的SQL Server本地数据库,其中包含一个名为 drink
的表。
我使用Microsoft Visual Studio 2008。
I have created a new SQL Server local database with a table called drink
.
I use 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 image
column but I don't know how to do.
我需要这个列,因为我想使用VB.NET显示DataGridView中的所有数据
I need this column, because I want to display all data in DataGridView using VB.NET
感谢您的帮助!
推荐答案
这适用于我...
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
这篇关于如何将图像插入到SQL Server数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文