[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 77
  • Last Modified:

VB.net SQL insert images in a folder into a SQL table

Hi

I have a folder on my desktop called "MyPics". I have a SQL table called Pics with three columns: ID, Pic, Detail.
What VB.net code would I use to insert all the pics in my folder into the SQL table in my Windows Forms application?

Thanks
0
Murray Brown
Asked:
Murray Brown
  • 2
1 Solution
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks, but I was looking for VB.net code
0
 
Máté FarkasDatabase Developer and AdministratorCommented:
Dim Conn As SqlConnection = New SqlConnection("<connection_string_to_sql_database>")
Dim Comm As SqlCommand = New SqlCommand("INSERT INTO Pics(Pic) Values(@Pic)", Conn)
Dim Stream As FileStream = New FileStream("<full_path_to_file>", FileMode.Open, FileAccess.Read)
Dim Reader As BinaryReader = New BinaryReader(Stream)
Dim Photo() As Byte = Reader.ReadBytes(Stream.Length)

Reader.Close()
Stream.Close()

Comm.Parameters.Add("@Pic",  SqlDbType.Image, Photo.Length).Value = Photo
Conn.Open()
Comm.ExecuteNonQuery()
Conn.Close()

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now