插入图像到SQL [英] Insert aan image to sql
问题描述
我想将几个图像文件添加到sql并在vb上访问它.而且,当应用程序处于运行模式时,我必须从vb将图像文件添加到sql ...
我该怎么做?请帮助我...
I want to add several image file to sql and access it on vb.. and also when the application is in run mode, i have to add image files to sql from vb...
How can i do it? please help me...
推荐答案
轻松-将SQL列声明为VARBINARY(MAX).
然后,如果图像在文件中:
Easy - Declare the SQL column as VARBINARY(MAX).
Then if the image is in a file:
Dim bytes As Byte() = File.ReadAllBytes(path)
如果在Image类中:
If it is in an Image class:
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim bytes As Byte() = ms.ToArray()
然后,您所要做的就是将行插入到数据库中:
Then all you have to do is insert the row to the DB:
Using con As New SqlConnection(strConnect)
con.Open()
Using com As New SqlCommand("INSERT INTO myTable (myImageColumn) VALUES (@IM)", con)
com.Parameters.AddWithValue("@IM", bytes)
com.ExecuteNonQuery()
End Using
End Using
我唯一要说的是,如果这些是大图像,我不会这样做-我会存储小缩略图,以及磁盘上的一个位置,我可以从该位置访问图像本身.图像很快变大,并且数据库的大小受到限制.
The only thing I would say is that if these are large images, I wouldn''t do it - I''d store small thumbnails, and a location on disk from which I could access the image itself. Images get big quickly, and Database are of limited size.
这篇关于插入图像到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!