VB.net save a picture to a SQL Server database (ii)

Source: Internet
Author: User
Tags tostring
server| Data | Database 4, declaring class-level variables. Find the code section generated by the design period and add the following statement to the variable declaration. What the? I don't know where the variable declaration is.

Private FS as FileStream

Private DS as DataSet

Private Conn as New SqlConnection ("server=localhost;database=northwind;integrated security=true;")

Private Currentpos as Integer = 9



5, began to write the code. First, Form_Load.

Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load



btnopen.enabled = True

btnsave.enabled = True

btnback.enabled = False

btnforward.enabled = False

End Sub

6, the click of the Open button event code:

Private Sub Openbtn_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnopen.click

Dim Opendlg as New OpenFileDialog

Opendlg.title = "Select a picture file"

Opendlg.filter = "(*.bmp) |*bmp| (*.jpg) |*.jpg "

Opendlg.showdialog ()

Lblpath.text = Opendlg.FileName.ToString ()

FS = New FileStream (Opendlg.FileName.ToString (), FileMode.Open, FileAccess.Read)

pictureBox1.Image = Image.FromFile (Opendlg.FileName.ToString ())

End Sub

7, the Save button click event code

Private Sub Savebtn_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnsave.click

Dim Sqlcomm as New SqlCommand

Sqlcomm.commandtext = "INSERT into employees (Lastname,firstname,photo) VALUES (@lastname, @firstname, @photo)"

Sqlcomm. Connection = conn

Dim Data (fs. Length) as Byte

Fs. Read (Data, 0, Int (FS). Length))

Dim Prm1 as New SqlParameter ("@lastname", Txtln.text)

Dim Prm2 as New SqlParameter ("@firstname", Txtfn.text)

Dim prm3 as New SqlParameter ("@photo", SqlDbType.VarBinary, Int (FS). Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data

Sqlcomm. Parameters.Add (PRM1)

Sqlcomm. Parameters.Add (PRM2)

Sqlcomm. Parameters.Add (PRM3)

Try

Conn. Open ()

Sqlcomm. ExecuteNonQuery () ' Executes the INSERT statement

Conn. Close ()

Fs. Close ()

Catch ex as Exception

MsgBox (ex. Message)

End Try

End Sub

8, select the event code view state

Private Sub checkbox1_checkedchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Checkbox1.checke Dchanged

If checkbox1.checked = True Then

btnopen.enabled = False

btnsave.enabled = False

btnback.enabled = True

btnforward.enabled = True

Currentpos = 9

Dim Sqlcomm as New SqlCommand

Sqlcomm.commandtext = "Select EmployeeId, photo from Employees order by EmployeeId"

Sqlcomm. Connection = conn

Dim da as New SqlDataAdapter (Sqlcomm)

Try

Conn. Open ()

ds = New DataSet

Da. Fill (ds, "Employees")

Conn. Close ()

Catch Sqlex as SqlException

MsgBox (Sqlex.message)

End Try



Dim data () as Byte = ds. Tables ("Employees"). Rows (9) ("Photo")

Dim Stmphoto as New MemoryStream (data)

pictureBox1.Image = Image.fromstream (Stmphoto)

Else

btnopen.enabled = True

btnsave.enabled = True

btnback.enabled = False

btnforward.enabled = False

End If

End Sub

9, ">>" button click event code



Private Sub Forward_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnforward.click

If Currentpos = ds. Tables ("Employees"). Rows.count-1 Then

Return

Else

Currentpos + 1

Dim data () as Byte

data = ds. Tables ("Employees"). Rows (Currentpos) ("Photo")

Dim Stmphoto as New MemoryStream (data)

pictureBox1.Image = Image.fromstream (Stmphoto)

End If

End Sub

10, "<<" button click event code

Private Sub Back_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnback.click

If Currentpos = 9 Then

Return

Else

Currentpos-= 1

End If



Dim Data () as Byte

Data = ds. Tables ("Employees"). Rows (Currentpos) ("Photo")

Dim Stmphoto as New MemoryStream (Data)

pictureBox1.Image = Image.fromstream (Stmphoto)

End Sub

11, OK, can run to see.






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.