Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports System.Net
Public Class 楦頭圖片資料庫
    Public Property Credentials As ICredentials
    ReadOnly conn As New SqlConnection : ReadOnly conn1 As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand : ReadOnly cmd1 As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim TT As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_圖片清單()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 楦頭編號, 楦頭圖片編號 FROM 楦頭圖片庫 ORDER BY 楦頭圖片編號"
        Set_DGV1載入後設定() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 120 : DataGridView1.Columns(1).Width = 100
    End Sub
    Private Sub 楦頭圖片資料庫_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        Set_圖片清單()
        TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox7.Visible = False
        Target1 = Target & AA(13) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1(0, e.RowIndex).Value.ToString
            TextBox2.Text = DataGridView1(1, e.RowIndex).Value.ToString
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 楦頭圖片 FROM 楦頭圖片庫 WHERE 楦頭圖片編號 LIKE N'" & TextBox2.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            While dr.Read() = True
                Dim unused As Byte() = New Byte(-1) {}
                Dim bytes As Byte() = DirectCast(dr.Item("楦頭圖片"), Byte())
                Dim oStream As New MemoryStream(bytes)
                PictureBox1.Image = Bitmap.FromStream(oStream)
            End While
            conn.Close()
            PictureBox1.SizeMode = 4
        End If
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        PA = InputBox("請輸入關鍵字")
        Set_DGV1載入前設定()
        SQL1 = "SELECT 楦頭編號, 楦頭圖片編號 FROM 楦頭圖片庫 WHERE (楦頭編號 LIKE N'%" & PA & "%') ORDER BY   楦頭圖片編號"
        Set_DGV1載入後設定() : Set_grid()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If TextBox1.Text = "" Then
            MsgBox("選擇要下載的圖片")
        Else
            With SaveFileDialog1 : .Filter = "所有文件(*.*)|*.*" : End With
            TT = TextBox2.Text

            SaveFileDialog1.FileName = TT & "-" & TextBox1.Text & ".jpg"

            SaveFileDialog1.ShowDialog() : TextBox3.Text = SaveFileDialog1.FileName
            ds1.Clear()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 楦頭圖片 FROM 楦頭圖片庫 WHERE 楦頭編號 LIKE N'" & TextBox1.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : conn.Close()
            If ds1.Tables(0).Rows.Count > 0 Then
                Dim imgData() As Byte
                imgData = ds1.Tables(0).Rows(0).Item("楦頭圖片")
                Dim fs As FileStream
                fs = File.Create(TextBox3.Text, imgData.Length - 1)
                fs.Write(imgData, 0, imgData.Length - 1)
                fs.Close()
            End If
            TextBox3.Text = ""
            MsgBox("下載完成")
        End If
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        If TextBox3.Text = "" Then
            MsgBox("請確定圖片資料已準備完畢")
        Else
            conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 楦頭圖片庫 SET  楦頭圖片 = BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox3.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE " &
                   "WHERE (楦頭圖片編號 = '" & TextBox2.Text & "') AND (楦頭編號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
            frq = CType(WebRequest.Create(New Uri(Target1 & TextBox3.Text)), FtpWebRequest)
            fcr = New NetworkCredential(FTP帳號, FTP密碼)
            frq.Credentials = fcr
            frq.Method = WebRequestMethods.Ftp.DeleteFile
            frq.UseBinary = True
            frp = CType(frq.GetResponse, FtpWebResponse)
            frp.Close()
            WebBrowser1.Refresh()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox3.Text = ""
            MsgBox("修改完成")
            Set_圖片清單()
        End If
    End Sub
    Private Sub TextBox3_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox3.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox3.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox7.Text = TextBox3.Text
        If TextBox3.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox7.Text)
                If Strings.Right((TextBox7.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox7.Text = ""
                        TextBox3.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox3.Text), STR2)
                    TextBox3.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox3_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox3.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox3.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
End Class