Option Strict Off
Imports System.Data.SqlClient
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 : ReadOnly ds2 As New DataSet
    Dim dr As SqlDataReader
    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_清單1()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 楦頭規格控制表.楦頭編號, 楦頭圖片庫.楦頭圖片編號 FROM 楦頭規格控制表 LEFT OUTER JOIN 楦頭圖片庫 ON 楦頭規格控制表.楦頭編號 = 楦頭圖片庫.楦頭編號 " &
               "WHERE (楦頭圖片庫.楦頭圖片編號 IS NULL or 楦頭圖片庫.楦頭圖片編號 LIKE N'') ORDER BY 楦頭規格控制表.楦頭編號"
        Set_DGV1載入後設定() : 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_清單1()
        TextBox1.Enabled = False : TextBox2.Enabled = False
        TextBox3.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
        End If
    End Sub
    Private Sub Button31_Click(sender As Object, e As EventArgs) Handles Button31.Click
        PA = InputBox("請輸入要查詢的資料")
        Set_DGV1載入前設定()
        SQL1 = "SELECT 楦頭規格控制表.楦頭編號, 楦頭圖片庫.楦頭圖片編號 FROM 楦頭規格控制表 LEFT OUTER JOIN 楦頭圖片庫 ON 楦頭規格控制表.楦頭編號 = 楦頭圖片庫.楦頭編號 " &
               "WHERE (楦頭圖片庫.楦頭圖片編號 IS NULL) AND (楦頭規格控制表.楦頭編號 LIKE N'%" & PA & "%') ORDER BY   楦頭規格控制表.楦頭編號"
        Set_DGV1載入後設定() : DataGridView1.Columns(0).Width = 120 : DataGridView1.Columns(1).Width = 100
        PA = ""
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If TextBox1.Text = "" Then
            MsgBox("楦頭編號沒有選擇")
        Else
            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
            If dr.Read() Then
                MsgBox("資料重複，請從新輸入或檢查")
                conn.Close() : dr.Close()
            Else
                conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP (1) 楦頭圖片編號 FROM 楦頭圖片庫 ORDER BY 楦頭圖片編號 DESC"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then : TextBox2.Text = dr("楦頭圖片編號").ToString : Else : TextBox2.Text = "0" : End If
                conn.Close()
                Dim NUM1 As Integer
                NUM1 = Double.Parse(Strings.Right(TextBox2.Text, 8)) + 1
                If NUM1 < 10 Then : TextBox2.Text = "SP" & "0000000" & NUM1
                ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox2.Text = "SP" & "000000" & NUM1
                ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox2.Text = "SP" & "00000" & NUM1
                ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox2.Text = "SP" & "0000" & NUM1
                ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox2.Text = "SP" & "000" & NUM1
                ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox2.Text = "SP" & "00" & NUM1
                ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox2.Text = "SP" & "0" & NUM1
                ElseIf NUM1 > 9999999 Then : TextBox2.Text = "SP" & NUM1
                End If

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 楦頭圖片庫 (楦頭編號, 楦頭圖片編號, 楦頭圖片) SELECT '" & TextBox1.Text & "','" & TextBox2.Text & "', " &
                       "BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 楦頭規格控制表 SET  楦頭圖號碼 = '" & TextBox2.Text & "' WHERE (楦頭編號 = '" & TextBox1.Text & "')"
                cmd.Connection = conn : 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()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox1.Text = "" : TextBox2.Text = "" : TextBox4.Text = ""
                Set_清單1()
                MsgBox("新增完成")
            End If
        End If
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        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
        TextBox3.Text = TextBox4.Text
        If TextBox4.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox3.Text)
                If Strings.Right((TextBox3.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox3.Text = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox3.Text = Strings.Left((TextBox3.Text), STR1)
                    End If
                Else
                    TextBox3.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox3.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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
End Class