窗体设计
- <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
- Partial Class Form6
- Inherits System.Windows.Forms.Form
- 'Form 重写 Dispose,以清理组件列表。
- <System.Diagnostics.DebuggerNonUserCode()> _
- Protected Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing AndAlso components IsNot Nothing Then
- components.Dispose()
- End If
- MyBase.Dispose(disposing)
- End Sub
- 'Windows 窗体设计器所必需的
- Private components As System.ComponentModel.IContainer
- '注意: 以下过程是 Windows 窗体设计器所必需的
- '可以使用 Windows 窗体设计器修改它。
- '不要使用代码编辑器修改它。
- <System.Diagnostics.DebuggerStepThrough()> _
- Private Sub InitializeComponent()
- Me.TextBox1 = New System.Windows.Forms.TextBox
- Me.Button1 = New System.Windows.Forms.Button
- Me.Button2 = New System.Windows.Forms.Button
- Me.PictureBox1 = New System.Windows.Forms.PictureBox
- Me.ListView1 = New System.Windows.Forms.ListView
- Me.id = New System.Windows.Forms.ColumnHeader
- CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
- Me.SuspendLayout()
- '
- 'TextBox1
- '
- Me.TextBox1.Location = New System.Drawing.Point(12, 12)
- Me.TextBox1.Name = "TextBox1"
- Me.TextBox1.Size = New System.Drawing.Size(421, 21)
- Me.TextBox1.TabIndex = 0
- '
- 'Button1
- '
- Me.Button1.Location = New System.Drawing.Point(439, 12)
- Me.Button1.Name = "Button1"
- Me.Button1.Size = New System.Drawing.Size(75, 23)
- Me.Button1.TabIndex = 1
- Me.Button1.Text = "Select"
- Me.Button1.UseVisualStyleBackColor = True
- '
- 'Button2
- '
- Me.Button2.Location = New System.Drawing.Point(520, 12)
- Me.Button2.Name = "Button2"
- Me.Button2.Size = New System.Drawing.Size(75, 23)
- Me.Button2.TabIndex = 1
- Me.Button2.Text = "UpLoad"
- Me.Button2.UseVisualStyleBackColor = True
- '
- 'PictureBox1
- '
- Me.PictureBox1.Location = New System.Drawing.Point(12, 51)
- Me.PictureBox1.Name = "PictureBox1"
- Me.PictureBox1.Size = New System.Drawing.Size(583, 422)
- Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
- Me.PictureBox1.TabIndex = 2
- Me.PictureBox1.TabStop = False
- '
- 'ListView1
- '
- Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.id})
- Me.ListView1.FullRowSelect = True
- Me.ListView1.GridLines = True
- Me.ListView1.Location = New System.Drawing.Point(601, 12)
- Me.ListView1.Name = "ListView1"
- Me.ListView1.Size = New System.Drawing.Size(277, 461)
- Me.ListView1.TabIndex = 3
- Me.ListView1.UseCompatibleStateImageBehavior = False
- Me.ListView1.View = System.Windows.Forms.View.Details
- '
- 'id
- '
- Me.id.Text = "id"
- Me.id.Width = 225
- '
- 'Form6
- '
- Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
- Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
- Me.ClientSize = New System.Drawing.Size(890, 485)
- Me.Controls.Add(Me.ListView1)
- Me.Controls.Add(Me.PictureBox1)
- Me.Controls.Add(Me.Button2)
- Me.Controls.Add(Me.Button1)
- Me.Controls.Add(Me.TextBox1)
- Me.Name = "Form6"
- Me.Text = "Form6"
- CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
- Me.ResumeLayout(False)
- Me.PerformLayout()
- End Sub
- Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Friend WithEvents Button2 As System.Windows.Forms.Button
- Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
- Friend WithEvents ListView1 As System.Windows.Forms.ListView
- Friend WithEvents id As System.Windows.Forms.ColumnHeader
- End Class
程序代码:
- Imports System.IO
- Imports System.Data
- Imports System.Data.SqlClient
- Public Class Form6
- Private connstr As String = "Server=(local);packet size=4096;Database=testwater;User ID=sa;Password=;Trusted_Connection=False"
- Private Conn As New SqlConnection(connstr)
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim frm As New OpenFileDialog
- frm.Filter = "图片(jpg)|*.jpg|图片(bmp)|*.bmp|其它|*.*"
- frm.ShowDialog()
- 'If frm.ShowDialog() = Me.Windows.Forms.DialogResult.OK Then
- TextBox1.Text = frm.FileName
- If frm.FileName <> "" Then
- PictureBox1.Image = System.Drawing.Image.FromFile(frm.FileName)
- End If
- loadlist()
- End Sub
- Private Sub loadlist()
- If ListView1.Items.Count > 0 Then
- ListView1.Items.Clear()
- End If
- Dim sql As String
- Dim ds As New DataSet
- sql = "select * from [test] order by id asc"
- Dim cmd As New SqlDataAdapter(sql, Conn)
- cmd.Fill(ds, "科")
- Dim myvalues(0) As String
- Dim listview As ListViewItem
- For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
- myvalues(0) = ds.Tables("科").Rows(i).Item("id")
- listview = New ListViewItem(myvalues)
- ListView1.Items.Add(listview)
- Next
- ds.Tables("科").Reset()
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- Try
- Dim Stream As New IO.MemoryStream
- PictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp)
- Stream.Flush()
- Dim Bytes As Byte() = Stream.ToArray
- Conn.Open()
- Dim SQL As String = "insert into [test] (img) values (@bytes) "
- Dim command As New SqlClient.SqlCommand(SQL, Conn)
- command.Parameters.Add("@bytes", SqlDbType.Image).Value = Bytes
- command.ExecuteNonQuery()
- Conn.Close()
- MsgBox("图片上传成功!")
- loadlist()
- Catch ex As Exception
- MsgBox(ex.Message.ToString + "图片上传失败!")
- End Try
- End Sub
- Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
- With ListView1.SelectedItems.Item(0)
- showpic("select img from test where id = " & CStr(.SubItems(0).Text))
- End With
- End Sub
- Private Sub showpic(ByVal sql)
- Try
- 'Conn.Open()
- Dim ds As New DataSet
- Dim Command As New SqlDataAdapter(sql, Conn)
- Command.Fill(ds, "图")
- Dim Bytes As Byte() = ds.Tables("图").Rows(0).Item("img")
- ds.Tables("图").Reset()
- Dim mStream As New IO.MemoryStream
- mStream.Write(Bytes, 0, Bytes.Length)
- mStream.Flush()
- Dim Img As New Bitmap(mStream)
- PictureBox1.Image = Img
- 'Conn.Close()
- Catch ex As Exception
- Conn.Close()
- MsgBox(ex.Message.ToString)
- End Try
- End Sub
- Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- loadlist()
- End Sub
- End Class
运行结果: