VB.NET Save Pictures in SQL Server Database
Yuan Zhi
4, declare the variable of the class. Find the code part generated during the design period, add the following statement to the variable declaration. what? I don't know where the variable is declared? No!
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, start writing code. First of all, Form_Load
Private Sub Form1_Load (Byval E 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, click event code for the Open button:
Private sub Openbtn_Click (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. Click event code for the Save button
Private sub savebtn_click (Byval E 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 () 'Performing insert statement
CONN.CLOSE ()
fs.close ()
Catch exception
MSGBOX (ex.Message)
END TRY
End Sub
8, select event code for the View status
Private sub checkbox1_checkedchanged (byval sender as system.Object, byval e as system.eventargs) handles checkbox1.checkedchanged
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 E 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 ("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 e 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 ("photo")
Dim Stmphoto as new memoryStream (data)
Picturebox1.image = image.fromstream (stmphoto)
End Sub
11, ok, you can run it.