照片存入数据库中C#(SQL)

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
        '从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)
        MessageBox.Show("导出成功")
    End Sub

    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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xjzdr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值