Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
Module SQL_Module
    '---------------資料庫權限語法--------------------------------------------------------------------------------------------------------------------------------------------------------
    Public 權限數量 As Integer = 128
    Public 系統權限 As String = "CC01, CC02, CC03, CC04, CC05, CC06, CC07, CC08, CC09, CC10, CC11, CC12, CC13, CC14, CC15, CC16, CC17, 
                                 CC18, CC19, CC20, CC21, CC22, CC23, CC24, CC25, CC26, CC27, CC28, CC29, CC30, CC31, CC32, CC33, CC34, CC35, CC36, CC37, CC38, CC39, CC40, CC41, CC42, CC43, 
                                 CC44, CC45, CC46, CC47, CC48, CC49, CC50, CC51, CC52, CC53, CC54, CC55, CC56, CC57, CC58, CC59, CC60, CC61, CC62, CC63, CC64, CC65, CC66, CC67, CC68, CC69, 
                                 CC70, CC71, CC72, CC73, CC74, CC75, CC76, CC77, CC78, CC79, CC80, CC81, CC82, CC83, CC84, CC85, CC86, CC87, CC88, CC89, CC90, CC91, CC92, CC93, CC94, CC95, 
                                 CC96, CC97, CC98, CC99, CC100, CC101, CC102, CC103, CC104, CC105, CC106, CC107, CC108, CC109, CC110, CC111, CC112, CC113, CC114, CC115, CC116, CC117, CC118, 
                                 CC119, CC120, CC121, CC122, CC123, CC124, CC125, CC126, CC127, CC128"
    Public 登入權限 As String = "姓名, 帳號, 密碼, 部門, 課別, 組別, 職稱, 密碼日期, " & 系統權限
    Public 登入權限1 As String = "姓名, 帳號, 密碼, 部門, 處別, 課別, 組別, 職稱, 密碼日期, 級別, NAS帳號, NAS密碼, " & 系統權限
    '---------------資料庫連線字串宣告--------------------------------------------------------------------------------------------------------------------------------------------------------
    Public Access1 As New OleDbConnection : Public AccesCmd As OleDbCommand
    Public Acdr As OleDbDataReader
    Public ConString, ConString1, ConString2, ConString3, ConString4 As String : Public AccessConString As String
    Public SQL1, SQL2, SQL3, SQL4, SQL5, SQL6, SQL7, SQL8, SQL9, SQL10, 列印用SQL As String
    Public conn As New SqlConnection : Public cmd As New SqlCommand
    Public dr As SqlDataReader : Public da As New SqlDataAdapter
    Friend Sub ConnOpen()
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Friend Sub ConnOpen_工程SQL()
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString1 : conn.Open() : End If
    End Sub
    Friend Sub ConnOpen_圖庫SQL()
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString2 : conn.Open() : End If
    End Sub
    Friend Sub ConnOpenFTP()
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString4 : conn.Open() : End If
    End Sub
    Friend Sub CmdSet_For_DGV()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd
    End Sub
    Friend Sub CmdSet_For_dr()
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
    End Sub
    Friend Sub CmdSet_For_dr_pic()
        Dim fs As FileStream : fs = File.OpenRead(PA49) : Dim picb(fs.Length) As Byte : fs.Read(picb, 0, fs.Length) : fs.Close() : cmd.Parameters.Clear()
        cmd.CommandText = SQL1 : cmd.Parameters.Add("@binary_file", SqlDbType.VarBinary).Value = picb : cmd.Connection = conn : dr = cmd.ExecuteReader
    End Sub
    '---------------系統應用模組--------------------------------------------------------------------------------------------------------------------------------------------------------
    Public 平均數 As Integer : Public 最終數 As Integer = 0 : Public 資料數 As Integer = 0
    Public Sub 查詢DGV中的關鍵字_手動(ByVal dgv As DataGridView, ByVal txtKeyword As TextBox)
        Dim keyword As String = txtKeyword.Text
        Dim found As Boolean = False
        dgv.ClearSelection()        ' 清除先前的选择
        Dim numColumns As Integer = dgv.Columns.Count        ' 获取 DataGridView 的列数
        Dim startRowIndex As Integer = If(lastFoundRowIndex = -1, 0, lastFoundRowIndex + 1)
        For colIndex As Integer = If(lastFoundRowIndex = -1, 0, lastFoundColIndex) To numColumns - 1
            For rowIndex As Integer = startRowIndex To dgv.Rows.Count - 1
                Dim cellValue As String = dgv.Rows(rowIndex).Cells(colIndex).Value.ToString()

                If cellValue.Contains(keyword) Then
                    dgv.Rows(rowIndex).Selected = True : dgv.FirstDisplayedScrollingRowIndex = rowIndex : found = True
                    lastFoundRowIndex = rowIndex : lastFoundColIndex = colIndex : Exit For
                End If
            Next : If found Then : Exit For : End If
        Next
        If Not found Then
            lastFoundRowIndex = -1 : lastFoundColIndex = -1 : dgv.FirstDisplayedScrollingRowIndex = 0 : MsgBox("整個表都搜尋完了!!") : 重新計算 = True : dgv.ClearSelection()
        Else : 重新計算 = False
        End If
    End Sub
    Public Sub 查詢DGV中的關鍵字_自動(ByVal dgv As DataGridView, ByVal txtKeyword As TextBox)
        Dim keyword As String = txtKeyword.Text
        Dim found As Boolean = False
        dgv.ClearSelection()        ' 清除先前的选择
        Dim numColumns As Integer = dgv.Columns.Count        ' 获取 DataGridView 的列数
        Dim startRowIndex As Integer = If(lastFoundRowIndex = -1, 0, lastFoundRowIndex + 1)
        For colIndex As Integer = If(lastFoundRowIndex = -1, 0, lastFoundColIndex) To numColumns - 1
            For rowIndex As Integer = startRowIndex To dgv.Rows.Count - 1
                Dim cellValue As String = dgv.Rows(rowIndex).Cells(colIndex).Value.ToString()

                If cellValue.Contains(keyword) Then
                    dgv.Rows(rowIndex).Selected = True : dgv.FirstDisplayedScrollingRowIndex = rowIndex : found = True
                    lastFoundRowIndex = rowIndex : lastFoundColIndex = colIndex : Exit For
                End If
            Next : If found Then : Exit For : End If
        Next
        If Not found Then
            lastFoundRowIndex = -1 : lastFoundColIndex = -1 : dgv.FirstDisplayedScrollingRowIndex = 0 : 重新計算 = True : dgv.ClearSelection()
        Else : 重新計算 = False
        End If
    End Sub
    Public Sub CB選擇清單(ByVal 輸入cb As ComboBox, Optional ByVal 原來值 As String = "")
        Dim 原來的 As String = 原來值
        For I As Integer = 0 To 3000 : PPBB(I) = "" : Next
        For I As Integer = 0 To 輸入cb.Items.Count - 1 : PPBB(I) = 輸入cb.Items(I).ToString : NN1 = I : Next
        If 輸入cb.Items.Count < 23 Then : 下拉清單_2.ShowDialog() : Else : 下拉清單_1.ShowDialog() : End If
        If PPAA = "" Then : 輸入cb.Text = 原來的
        Else : 輸入cb.Text = PPAA : End If
    End Sub
    Public Sub CB選擇清單_可選空白(ByVal 輸入cb As ComboBox)
        For I As Integer = 0 To 3000 : PPBB(I) = "" : Next
        For I As Integer = 0 To 輸入cb.Items.Count - 1 : PPBB(I) = 輸入cb.Items(I).ToString : NN1 = I : Next
        If 輸入cb.Items.Count < 23 Then : 下拉清單_2.ShowDialog() : Else : 下拉清單_1.ShowDialog() : End If
        輸入cb.Text = PPAA
    End Sub
    Public Class MyMod
        Friend Shared Sub 進度條()
            平均數 = 10000 / 資料數
            最終數 += 平均數
            If 最終數 > 10000 Then : 最終數 = 10000 : End If
            FH_ERP_SYS.ToolStripProgressBar1.Value = 最終數
        End Sub
    End Class
End Module
