Solved

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

Posted on 2016-09-21
3
53 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

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