Add images to the SQL Server database (VB. NET)

Source: Internet
Author: User
Original address: http://dotnet.aspx.cc/ShowDetail.aspx? J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3K

I took out the key parts and made relevant modifications:

Picturebox1 is a picturebox control.

Name is varchar (50) type image is image type

Private   Sub insertimage () Sub Insertimage () ' Insert images into the database
Dim Mystream As   New Filestream ( " C: inetpubwwwrootwin2000.gif " , Filemode. Open, fileaccess. Read)
Dim Buffer (mystream. length) As   Byte
Mystream. Read (buffer, 0 , Mystream. length)
Mystream. Close ()

Dim Sqlconnectionstring As   String   =   " Persist Security info = false; Data Source =.; initial catalog = AA; user id = sa; Password ="
Dim Myconnection As   New Sqlconnection (sqlconnectionstring)
Dim Mycommand As   New Sqlcommand ( " Insert into Photos ([name], [Photo]) values (@ name, @ image) " , Myconnection)
Mycommand. Parameters. Add ( " @ Name " , " Test " )
Mycommand. Parameters. Add ( " @ Image " , Buffer)
Myconnection. open ()
Mycommand. executenonquery ()
Myconnection. Close ()
End sub


Private   Sub showimage () Sub Showimage () ' Retrieve images from the database
Dim Sqlconnectionstring As   String   =   " Persist Security info = false; Data Source =.; initial catalog = AA; user id = sa; Password ="
Dim Myconnection As   New Sqlconnection (sqlconnectionstring)
Myconnection. open ()
Dim Mycommand As   New Sqlclient. sqlcommand ( " Select photo from photos where [name] = 'test' " , Myconnection)
Dim Buffer () As   Byte   = Mycommand. executescalar ()
If ( Not Buffer Is   Nothing ) Andalso (Buffer. Length >   0 ) Then
Dim Stream As   New Memorystream (buffer, 0 , Buffer. length, True )
Picturebox1.image =   New Bitmap (Stream)
Stream. Close ()
End   If
Myconnection. Close ()
End sub
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.