Solved

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

Posted on 2016-09-21
3
46 Views
Last Modified: 2016-09-21
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
Comment
Question by:murbro
  • 2
3 Comments
 

Author Comment

by:murbro
ID: 41808271
Hi. Thanks, but I was looking for VB.net code
0
 
LVL 12

Accepted Solution

by:
Máté Farkas earned 500 total points
ID: 41808427
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
 

Author Closing Comment

by:murbro
ID: 41808432
thanks very much
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question