Vb. NET operations SQL Server binary data

Source: Internet
Author: User
Tags insert
server| Binary | Data in the VB period, to insert binary data into SQL SERVER, is through the ADODB. Stream implementation, in. NET, the operation of the "flow" format is more powerful and simple, this demonstrates the ability to insert data into SQL SERVER and read out.






Add a OpenFileDialog control to the form, two PictureBox, with the following code:
--------------------------------------------------------------------------------------------
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub button4_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button4.click
Me.dispose (True)
End Sub

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
If Ofdpic.showdialog = DialogResult.OK Then
Pbpreview.image = Image.FromFile (ofdpic.filename)
End If
End Sub

Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
If Pbpreview.image is nothing Then
MsgBox ("Please select pictures First", msgboxstyle.exclamation)
Exit Sub
End If
Dim fs as FileStream = New FileStream (ofdpic.filename, FileMode.Open, FileAccess.Read)
Dim BT (FS. Length) as Byte
Fs. Read (BT, 0, FS.) Length)
Fs. Close ()
FS = Nothing
Dim sqlconn as Sqlclient.sqlconnection = New sqlclient.sqlconnection ("server=" (local); User Id=sa; Password=123;database=pubs ")
Sqlconn.open ()
Dim SQLCMD as New sqlclient.sqlcommand ("Sp_insertimage", sqlconn)
Sqlcmd.commandtype = CommandType.StoredProcedure
SQLCMD.PARAMETERS.ADD ("@img", Sqldbtype.image). Value = BT
Sqlcmd.executenonquery ()
SQLCMD = Nothing
Sqlconn.close ()
Sqlconn = Nothing
MsgBox ("Picture inserted successfully", msgboxstyle.information)
End Sub

Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click
Dim sqlconn as Sqlclient.sqlconnection = New sqlclient.sqlconnection ("server=" (local); User Id=sa; Password=123;database=pubs ")
Sqlconn.open ()
Dim SQLCMD as New sqlclient.sqlcommand ("SELECT img from test WHERE t_id=3", sqlconn)
Sqlcmd.commandtype = CommandType.Text

Dim bt () as Byte = Sqlcmd.executescalar ()
If not BT are nothing Then
If BT. Length > 0 Then
Dim fs as MemoryStream = New MemoryStream (BT)
Pbreview.image = Image.fromstream (FS)
' FS. Close
' FS = Nothing
' You can try to remove the comments on the above two sentences to see what effect ^_^
Else
MsgBox ("No Pictures")
End If
Else
MsgBox ("No Data")
End If

SQLCMD = Nothing
Sqlconn.close ()
Sqlconn = Nothing
End Sub
End Class
-----------------------------------------------------------------------------
Database section:
----------------
Use pubs
Go
Create Table Test (t_id int identity (1,1), IMG image)
Go
Create Procedure Sp_insertimage
@img image
As
Insert into Test (IMG) Values (@img)
Go





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.