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 aa1 As MsgBoxResult
    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() : Set_grid()
    End Sub
    Private Sub Set_圖片清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 客戶, 形體號, 形體名, 顏色, 類別, 入圖者, 圖片編號 FROM 鞋型圖片資料庫 ORDER BY 客戶, 類別, 形體號, 形體名, 顏色"
        Else
            SQL1 = "SELECT 客戶, 形體號, 形體名, 顏色, 類別, 入圖者, 圖片編號 FROM 鞋型圖片資料庫 WHERE 客戶 LIKE N'%" & PA & "%' OR 形體號 LIKE N'%" & PA & "%' OR 形體名 LIKE N'%" & PA & "%' " &
                   "OR 顏色 LIKE N'%" & PA & "%' OR 類別 LIKE N'%" & PA & "%' OR 入圖者 LIKE N'%" & PA & "%' ORDER BY 客戶, 類別, 形體號, 形體名, 顏色"
        End If
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 90 : DataGridView1.Columns(1).Width = 110 : DataGridView1.Columns(2).Width = 110 : DataGridView1.Columns(3).Width = 90 : DataGridView1.Columns(4).Width = 50
        DataGridView1.Columns(5).Width = 50 : DataGridView1.Columns(6).Visible = False
    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
        TextBox1.Visible = False : TextBox2.Visible = False
        CheckBox4.Checked = True : TextBox7.Visible = False
        TextBox8.Enabled = False : TextBox3.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False
        Set_圖片清單()
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
        Target1 = Target & AA(1) & "/"
        WebBrowser1.Url = New Uri(Target1)
        If CC(12) = False Then : Button1.Enabled = False : End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox8.Text = DataGridView1(0, e.RowIndex).Value.ToString : TextBox3.Text = DataGridView1(1, e.RowIndex).Value.ToString
            TextBox5.Text = DataGridView1(2, e.RowIndex).Value.ToString : TextBox6.Text = DataGridView1(3, e.RowIndex).Value.ToString
            ComboBox2.Text = DataGridView1(4, e.RowIndex).Value.ToString : TextBox1.Text = DataGridView1(6, e.RowIndex).Value.ToString
            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 : dr = cmd.ExecuteReader
            PictureBox1.Image = Nothing
            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 Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Set_圖片清單()
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_圖片清單() : PA = ""
    End Sub
    Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.Click
        CheckBox4.Checked = True : CheckBox3.Checked = False : CheckBox2.Checked = False
        PA = "" : Set_圖片清單() : PA = ""
    End Sub
    Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.Click
        CheckBox4.Checked = False : CheckBox3.Checked = True : CheckBox2.Checked = False
        PA = "彩圖" : Set_圖片清單() : PA = ""
    End Sub
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.Click
        CheckBox4.Checked = False : CheckBox3.Checked = False : CheckBox2.Checked = True
        PA = "設計圖" : Set_圖片清單() : PA = ""
    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 = ComboBox2.Text

            SaveFileDialog1.FileName = TextBox8.Text & " - " & TT & " - " & TextBox5.Text & " - " & TextBox3.Text & " - " & TextBox6.Text & ".jpg"

            SaveFileDialog1.ShowDialog() : TextBox4.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(TextBox4.Text, imgData.Length - 1)
                fs.Write(imgData, 0, imgData.Length - 1)
                fs.Close()
            End If
            TextBox4.Text = "" : TextBox1.Text = ""
            MsgBox("下載完成")
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        aa1 = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 鞋型圖片資料庫 WHERE (圖片編號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            MsgBox("刪除完成")
        End If
        Set_圖片清單() : Set_grid()
    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
    Private Sub TextBox4_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox4.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox4.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 = TextBox4.Text
        If TextBox4.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 = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox4_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox4.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox4.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If CheckBox1.Checked = False Then
        Else
            SQL1 = "UPDATE 鞋型圖片資料庫 SET 圖片 = BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(1) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE " &
                   "WHERE (圖片編號 = '" & 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 & TextBox4.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()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox4.Text = "" : TextBox1.Text = ""
            MsgBox("修改完成")
        End If
    End Sub
End Class