将图片保存至MSSQL

窗体设计

 

  1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  2. Partial Class Form6
  3.     Inherits System.Windows.Forms.Form
  4.     'Form 重写 Dispose,以清理组件列表。
  5.     <System.Diagnostics.DebuggerNonUserCode()> _
  6.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  7.         If disposing AndAlso components IsNot Nothing Then
  8.             components.Dispose()
  9.         End If
  10.         MyBase.Dispose(disposing)
  11.     End Sub
  12.     'Windows 窗体设计器所必需的
  13.     Private components As System.ComponentModel.IContainer
  14.     '注意: 以下过程是 Windows 窗体设计器所必需的
  15.     '可以使用 Windows 窗体设计器修改它。
  16.     '不要使用代码编辑器修改它。
  17.     <System.Diagnostics.DebuggerStepThrough()> _
  18.     Private Sub InitializeComponent()
  19.         Me.TextBox1 = New System.Windows.Forms.TextBox
  20.         Me.Button1 = New System.Windows.Forms.Button
  21.         Me.Button2 = New System.Windows.Forms.Button
  22.         Me.PictureBox1 = New System.Windows.Forms.PictureBox
  23.         Me.ListView1 = New System.Windows.Forms.ListView
  24.         Me.id = New System.Windows.Forms.ColumnHeader
  25.         CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
  26.         Me.SuspendLayout()
  27.         '
  28.         'TextBox1
  29.         '
  30.         Me.TextBox1.Location = New System.Drawing.Point(12, 12)
  31.         Me.TextBox1.Name = "TextBox1"
  32.         Me.TextBox1.Size = New System.Drawing.Size(421, 21)
  33.         Me.TextBox1.TabIndex = 0
  34.         '
  35.         'Button1
  36.         '
  37.         Me.Button1.Location = New System.Drawing.Point(439, 12)
  38.         Me.Button1.Name = "Button1"
  39.         Me.Button1.Size = New System.Drawing.Size(75, 23)
  40.         Me.Button1.TabIndex = 1
  41.         Me.Button1.Text = "Select"
  42.         Me.Button1.UseVisualStyleBackColor = True
  43.         '
  44.         'Button2
  45.         '
  46.         Me.Button2.Location = New System.Drawing.Point(520, 12)
  47.         Me.Button2.Name = "Button2"
  48.         Me.Button2.Size = New System.Drawing.Size(75, 23)
  49.         Me.Button2.TabIndex = 1
  50.         Me.Button2.Text = "UpLoad"
  51.         Me.Button2.UseVisualStyleBackColor = True
  52.         '
  53.         'PictureBox1
  54.         '
  55.         Me.PictureBox1.Location = New System.Drawing.Point(12, 51)
  56.         Me.PictureBox1.Name = "PictureBox1"
  57.         Me.PictureBox1.Size = New System.Drawing.Size(583, 422)
  58.         Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
  59.         Me.PictureBox1.TabIndex = 2
  60.         Me.PictureBox1.TabStop = False
  61.         '
  62.         'ListView1
  63.         '
  64.         Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.id})
  65.         Me.ListView1.FullRowSelect = True
  66.         Me.ListView1.GridLines = True
  67.         Me.ListView1.Location = New System.Drawing.Point(601, 12)
  68.         Me.ListView1.Name = "ListView1"
  69.         Me.ListView1.Size = New System.Drawing.Size(277, 461)
  70.         Me.ListView1.TabIndex = 3
  71.         Me.ListView1.UseCompatibleStateImageBehavior = False
  72.         Me.ListView1.View = System.Windows.Forms.View.Details
  73.         '
  74.         'id
  75.         '
  76.         Me.id.Text = "id"
  77.         Me.id.Width = 225
  78.         '
  79.         'Form6
  80.         '
  81.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
  82.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  83.         Me.ClientSize = New System.Drawing.Size(890, 485)
  84.         Me.Controls.Add(Me.ListView1)
  85.         Me.Controls.Add(Me.PictureBox1)
  86.         Me.Controls.Add(Me.Button2)
  87.         Me.Controls.Add(Me.Button1)
  88.         Me.Controls.Add(Me.TextBox1)
  89.         Me.Name = "Form6"
  90.         Me.Text = "Form6"
  91.         CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
  92.         Me.ResumeLayout(False)
  93.         Me.PerformLayout()
  94.     End Sub
  95.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  96.     Friend WithEvents Button1 As System.Windows.Forms.Button
  97.     Friend WithEvents Button2 As System.Windows.Forms.Button
  98.     Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  99.     Friend WithEvents ListView1 As System.Windows.Forms.ListView
  100.     Friend WithEvents id As System.Windows.Forms.ColumnHeader
  101. End Class

程序代码:

 

 

  1. Imports System.IO
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Public Class Form6
  5.     Private connstr As String = "Server=(local);packet size=4096;Database=testwater;User ID=sa;Password=;Trusted_Connection=False"
  6.     Private Conn As New SqlConnection(connstr)
  7.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
  8.         Dim frm As New OpenFileDialog
  9.         frm.Filter = "图片(jpg)|*.jpg|图片(bmp)|*.bmp|其它|*.*"
  10.         frm.ShowDialog()
  11.         'If frm.ShowDialog() = Me.Windows.Forms.DialogResult.OK Then
  12.         TextBox1.Text = frm.FileName
  13.         If frm.FileName <> "" Then
  14.             PictureBox1.Image = System.Drawing.Image.FromFile(frm.FileName)
  15.         End If
  16.         loadlist()
  17.     End Sub
  18.     Private Sub loadlist()
  19.         If ListView1.Items.Count > 0 Then
  20.             ListView1.Items.Clear()
  21.         End If
  22.         Dim sql As String
  23.         Dim ds As New DataSet
  24.         sql = "select * from [test] order by id asc"
  25.         Dim cmd As New SqlDataAdapter(sql, Conn)
  26.         cmd.Fill(ds, "科")
  27.         Dim myvalues(0) As String
  28.         Dim listview As ListViewItem
  29.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  30.             myvalues(0) = ds.Tables("科").Rows(i).Item("id")
  31.             listview = New ListViewItem(myvalues)
  32.             ListView1.Items.Add(listview)
  33.         Next
  34.         ds.Tables("科").Reset()
  35.     End Sub
  36.     Private Sub Button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button2.Click
  37.         Try
  38.             Dim Stream As New IO.MemoryStream
  39.             PictureBox1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Bmp)
  40.             Stream.Flush()
  41.             Dim Bytes As Byte() = Stream.ToArray
  42.             Conn.Open()
  43.             Dim SQL As String = "insert into [test] (img) values (@bytes) "
  44.             Dim command As New SqlClient.SqlCommand(SQL, Conn)
  45.             command.Parameters.Add("@bytes", SqlDbType.Image).Value = Bytes
  46.             command.ExecuteNonQuery()
  47.             Conn.Close()
  48.             MsgBox("图片上传成功!")
  49.             loadlist()
  50.         Catch ex As Exception
  51.             MsgBox(ex.Message.ToString + "图片上传失败!")
  52.         End Try
  53.     End Sub
  54.     Private Sub ListView1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles ListView1.Click
  55.         With ListView1.SelectedItems.Item(0)
  56.             showpic("select img from test where id = " & CStr(.SubItems(0).Text))
  57.         End With
  58.     End Sub
  59.     Private Sub showpic(ByVal sql)
  60.         Try
  61.             'Conn.Open()
  62.             Dim ds As New DataSet
  63.             Dim Command As New SqlDataAdapter(sql, Conn)
  64.             Command.Fill(ds, "图")
  65.             Dim Bytes As Byte() = ds.Tables("图").Rows(0).Item("img")
  66.             ds.Tables("图").Reset()
  67.             Dim mStream As New IO.MemoryStream
  68.             mStream.Write(Bytes, 0, Bytes.Length)
  69.             mStream.Flush()
  70.             Dim Img As New Bitmap(mStream)
  71.             PictureBox1.Image = Img
  72.             'Conn.Close()
  73.         Catch ex As Exception
  74.             Conn.Close()
  75.             MsgBox(ex.Message.ToString)
  76.         End Try
  77.     End Sub
  78.     Private Sub Form6_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load
  79.         loadlist()
  80.     End Sub
  81. End Class

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值