Imports System.IO
Imports System.Data.SqlClIEnt
Public Class Form1
Inherits System.Windows.Forms.Form
Dim tempConnection As New SqlConnection("workstation id=ZDR-VHBW5MGJFVA;packet size=4096;user id=zdr;integrated security=SSPI;initial catalog=HLManager;persist security info=False")
Dim tempAdapter As SqlDataAdapter
Dim tempDataSet As New DataSet ''用於填充表格時使用
Dim tempDataSet2 As New DataSet ''用於保存照片時使用
Dim MyFileStream1 As FileStream ''照片形成的文件流
Dim MyJPGFileName As String
#Region " Windows 窗體設計器生成的代碼 "
Public Sub New()
MyBase.New()
''該調用是 Windows 窗體設計器所必需的。
InitializeComponent()
''在 InitializeComponent() 調用之後添加任何初始化
End Sub
''窗體重寫 dispose 以清理組件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''Windows 窗體設計器所必需的
Private components As System.ComponentModel.IContainer
''注意: 以下過程是 Windows 窗體設計器所必需的
''可以使用 Windows 窗體設計器修改此過程。
''不要使用代碼編輯器修改它。
FrIEnd WithEvents DataGrid1 As System.Windows.Forms.DataGrid
FrIEnd WithEvents Button1 As System.Windows.Forms.Button
FrIEnd WithEvents TextBox1 As System.Windows.Forms.TextBox
FrIEnd WithEvents Label1 As System.Windows.Forms.Label
FrIEnd WithEvents PictureBox1 As System.Windows.Forms.PictureBox
FrIEnd WithEvents Button2 As System.Windows.Forms.Button
FrIEnd WithEvents Label2 As System.Windows.Forms.Label
FrIEnd WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
FrIEnd WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
FrIEnd WithEvents Button3 As System.Windows.Forms.Button
FrIEnd WithEvents Button4 As System.Windows.Forms.Button
FrIEnd WithEvents Button5 As System.Windows.Forms.Button
FrIEnd WithEvents Button7 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button2 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.Button5 = New System.Windows.Forms.Button
Me.Button7 = New System.Windows.Forms.Button
CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
''
''DataGrid1
''
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(16, 32)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(272, 280)
Me.DataGrid1.TabIndex = 0
''
''Button1
''
Me.Button1.Location = New System.Drawing.Point(296, 72)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(216, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "查詢數據庫的記錄並幫定表格"
''
''TextBox1
''
Me.TextBox1.Location = New System.Drawing.Point(352, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(120, 21)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
''
''Label1
''
Me.Label1.Location = New System.Drawing.Point(352, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(128, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "請輸入人員的姓名:"
''
''PictureBox1
''
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(536, 8)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(192, 216)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 4
Me.PictureBox1.TabStop = False
''
''Button2
''
Me.Button2.Location = New System.Drawing.Point(296, 104)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(216, 23)
Me.Button2.TabIndex = 5
Me.Button2.Text = "導入照片到PictureBox中"
''
''Label2
''
Me.Label2.Location = New System.Drawing.Point(16, 8)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(192,23)
Me.Label2.TabIndex = 6
Me.Label2.Text = "Label2"
''
''Button3
''
Me.Button3.Location = New System.Drawing.Point(296, 136)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(216, 23)
Me.Button3.TabIndex = 7
Me.Button3.Text = "保存PictureBox中的照片到當前記錄"
''
''Button4
''
Me.Button4.Location = New System.Drawing.Point(296, 168)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(216, 23)
Me.Button4.TabIndex = 8
Me.Button4.Text = "從PictureBox中導出照片到文件"
''
''Button5
''
Me.Button5.Location = New System.Drawing.Point(296, 200)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(216, 23)
Me.Button5.TabIndex = 9
Me.Button5.Text = "直接從數據庫中導出照片到文件"
''
''Button7
''
Me.Button7.Location = New System.Drawing.Point(296,232)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(216, 23)
Me.Button7.TabIndex = 11
Me.Button7.Text = "照片文件直接到數據庫中"
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClIEntSize = New System.Drawing.Size(752, 485)
Me.Controls.Add(Me.Button7)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ''自動適應PictureBox的大小
OpenFileDialog1.Filter = "JPG圖像文件(*.jpg)|*.jpg
|BMP位圖文件(*.bmp)|*.bmp|全部文件(*.*)|*.*"
OpenFileDialog1.Multiselect = False ''設定不能可以選擇多個文件
SaveFileDialog1.Filter = "JPG圖像文件(*.jpg)|*.jpg|BMP位圖文件(*.bmp)|*.bmp"
'' Me.DataGrid1.DataGridTextBoxColumn.ReadOnly = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''查詢並顯示數據
tempDataSet.Clear()
tempConnection.Open()
If Me.TextBox1.Text.Length <> 0 Then
MessageBox.Show("SELECT * FROM HL_Photo where membername like ''" + Replace(Me.TextBox1.Text, "*", "%") + "''")
tempAdapter = New SqlDataAdapter("SELECT * FROM HL_Photo where membername like ''" + Replace(Me.TextBox1.Text, "*", "%") + "''", tempConnection)
Else
'' MessageBox.Show("SELECT * FROM HL_Photo")
tempAdapter = New SqlDataAdapter("SELECT * FROM HL_Photo", tempConnection)
End If
tempDataSet.Clear()
tempAdapter.Fill(tempDataSet)
tempConnection.Close()
Me.Label2.Text = "查詢的記錄數為:" + CStr(tempDataSet.Tables(0).Rows.Count)
Me.DataGrid1.DataSource = tempDataSet.Tables(0)
DataGrid1_Click(Nothing, Nothing) ''顯示第一條記錄的照片
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
''導入照片到PictureBox中
If DataGrid1.CurrentRowIndex < 0 Then
MessageBox.Show("請選擇記錄")
Exit Sub
End If
Me.Label2.Text = "當前人員的ID號是:" + CStr(Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString())
If Me.OpenFileDialog1.ShowDialog = DialogResult.Cancel Then
Exit Sub
End If
MyJPGFileName = Me.OpenFileDialog1.FileName
MyFileStream1 = New FileStream(MyJPGFileName, IO.FileMode.Open, IO.FileAccess.Read)
'' MessageBox.Show(CStr(MyFileStream1.Length))
Me.PictureBox1.Image = Image.FromFile(MyJPGFileName, True) ''.FromStream(, True)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
''保存PictureBox中的照片到當前記錄
If Me.OpenFileDialog1.FileName.Length = 0 Then
MessageBox.Show("沒有照片")
Exit Sub
End If
Dim fs = New System.IO.FileStream(MyJPGFileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim imgData(fs.Length - 1) As Byte
fs.Read(imgData, 0, fs.Length - 1)
fs.Close()
''照片保存到內存表中
tempDataSet.Tables(0).Rows(Me.DataGrid1.CurrentRowIndex).Item("MeMberPhoto") = imgData
''打開數據庫連接,取出數據
'' tempConnection.ConnectionString = "workstation id=ZDR-VHBW5MGJFVA;packet size=4096;user id=zdr;integrated security=SSPI;initial catalog=HLManager;persist security info=False"
tempConnection.Open()
tempAdapter = New SqlDataAdapter("SELECT * FROM HL_Photo where MemberID=" + Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString(), tempConnection)
tempDataSet2.Clear()
'' MessageBox.Show("SELECT * FROM HL_Photo where MemberID=" + Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString())
tempAdapter.Fill(tempDataSet2)
''更新數據
Dim cb As New SqlCommandBuilder(tempAdapter)
'' tempAdapter.Fill(tempDataSet2)
tempDataSet2.Tables(0).Rows(0).Item("MeMberPhoto") = imgData
tempAdapter.Update(tempDataSet2)
MessageBox.Show("照片保存成功")
tempConnection.Close()
cb = Nothing
imgData = Nothing
fs = Nothing
'' Button1_Click(Nothing, Nothing)
End Sub
Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
End Sub
Private Sub DataGrid1_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles DataGrid1.Click
If Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString() < 0 Then
Exit Sub
End If
''顯示當前記錄的照片
If tempDataSet.Tables(0).Rows(DataGrid1.CurrentRowIndex)("MeMberPhoto") Is DBNull.Value Then
Me.PictureBox1.Image = Nothing
Exit Sub
End If
Dim bImage() As Byte
bImage = tempDataSet.Tables(0).Rows(DataGrid1.CurrentRowIndex).Item("MeMberPhoto")
Dim ms As New System.IO.MemoryStream(bImage)
If ms.Length > 0 Then
Me.PictureBox1.Image = Image.FromStream(ms, True)
End If
bImage = Nothing
ms = Nothing
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click<br /> ''從PictureBox中導出照片到文件
If Me.PictureBox1.Image Is Nothing Then ''沒有照片時退出
Exit Sub
End If
If Me.SaveFileDialog1.ShowDialog = DialogResult.Cancel Then
Exit Sub
End If
Me.PictureBox1.Image.Save(Me.SaveFileDialog1.FileName)
&nb
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
''直接從數據庫中導出照片到文件
If Me.DataGrid1.DataSource Is Nothing Then
Exit Sub
End If
If Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString() < 0 Then ''沒有記錄時退出
Exit Sub
End If
If tempDataSet.Tables(0).Rows(DataGrid1.CurrentRowIndex)("MeMberPhoto") Is DBNull.Value Then ''有記錄沒有照片時退出
Exit Sub
End If
If Me.SaveFileDialog1.ShowDialog = DialogResult.Cancel Then
Exit Sub
End If
Dim bImage() As Byte
bImage = tempDataSet.Tables(0).Rows(DataGrid1.CurrentRowIndex).Item("MeMberPhoto")
Dim ms As New System.IO.MemoryStream(bImage)
If ms.Length > 0 Then
''Me.PictureBox1.Image = Image.FromStream(ms, True)
Dim aa As New System.IO.FileStream(SaveFileDialog1.FileName, FileMode.OpenOrCreate)
ms.WriteTo(aa) ''將內存流寫入到文件流中
aa = Nothing
MessageBox.Show("導出文件成功")/>
End If
bImage = Nothing
ms = Nothing
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
''照片直接到數據庫中(不經過PictureBox)
If Me.OpenFileDialog1.ShowDialog = DialogResult.Cancel Then
Exit Sub
End If
If Me.DataGrid1.DataSource Is Nothing Then
Exit Sub
End If
If Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString() < 0 Then ''沒有記錄時退出
Exit Sub
End If
''建立一個文件流
Dim Fs As New FileStream(Me.OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
'' Dim fs = New System.IO.FileStream(MyJPGFileName, IO.FileMode.Open, IO.FileAccess.Read)
''建立一個Byte類型的數組
Dim imgData(Fs.Length - 1) As Byte
'',將文件流存入到Byte類型的數組中
Fs.Read(imgData, 0, Fs.Length - 1)
''更新內存表中的照片
tempDataSet.Tables(0).Rows(Me.DataGrid1.CurrentRowIndex).Item("MeMberPhoto") = imgData
''更新PictureBOX中的圖片
MyJPGFileName = Me.OpenFileDialog1.FileName
'' MessageBox.Show(CStr(MyFileStream1.Length))
Me.PictureBox1.Image = Image.FromFile(MyJPGFileName, True)
''打開數據庫連接,取出數據
tempConnection.Open()
tempAdapter = New SqlDataAdapter("SELECT * FROM HL_Photo where MemberID=" + Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString(), tempConnection)
tempDataSet2.Clear()
tempAdapter.Fill(tempDataSet2)
''更新數據
Dim cb As New SqlCommandBuilder(tempAdapter)
'' tempAdapter.Fill(tempDataSet2)
tempDataSet2.Tables(0).Rows(0).Item("MeMberPhoto") = imgData
tempAdapter.Update(tempDataSet2)
MessageBox.Show("照片保存成功")
tempConnection.Close()
imgData = Nothing
Fs = Nothing
''Me.Button1_Click(Nothing, Nothing)
End Sub
End Class