Imports System.Data.SqlClient Imports System.IO Imports System.Net Imports System.Text Public Class Form1 Dim ConString, ConString2, SQL1, SQL2 As String Dim conn As New SqlConnection : Public conn1 As New SqlConnection : Public cmd As New SqlCommand : Public cmd1 As New SqlCommand Dim dr As SqlDataReader : Dim da As New SqlDataAdapter Dim DTP, DTP1, DTP2, DTP3, DTP4, DTP5, DTP6 As String Friend Sub ConnOpen() conn.Close() : If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If End Sub Friend Sub CmdSet_For_dr() cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader End Sub Private Sub 分類下拉表單資料載入() ConnOpen() : SQL1 = "SELECT 客戶 FROM 客戶IP讀取 ORDER BY 客戶" : CmdSet_For_dr() 客戶_tb.Items.Clear() : While (dr.Read()) : 客戶_tb.Items.Add(dr("客戶")) : End While : conn.Close() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Str As String = Application.StartupPath If File.Exists(Str + "\LIVE.txt") Then Dim filereader = My.Computer.FileSystem.OpenTextFileReader(Str + "\LIVE.txt", System.Text.Encoding.Default) Dim Text As String = "" : Dim line As String = filereader.ReadLine() While Not (line Is Nothing) : Text = line : line = filereader.ReadLine() : End While : filereader.Close() 客戶_tb.Text = Strings.Left(Text, 5) End If ConString = "Data Source=mssql-147629-0.cloudclusters.net,12711; Initial Catalog=GCM-IP-DATE02;Persist Security Info=True;User ID=b70340;Password=Lee0911274990;Max pool size = 200;Connection Timeout=0" Me.Text = "伺服器外網IP查詢 - 未啟動" End Sub Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown 分類下拉表單資料載入() : 查詢() : 啟動_bt.PerformClick() End Sub Private Sub 查詢() Try Dim client As New WebClient() ' 使用 WebClient 發送 HTTP GET 請求並獲取響應 Dim response As String = client.DownloadString("https://api.ipify.org") ' 解析響應以獲取外部 IP 地址 Dim externalIP As String = response.Trim() : IP_tb.Text = externalIP ' 將外部 IP 地址顯示在視窗上 Catch ex As Exception : MessageBox.Show("錯誤訊息: 執行外部IP查詢時出現錯誤(" & ex.Message & ")") : End Try End Sub Private Sub 啟動_bt_Click(sender As Object, e As EventArgs) Handles 啟動_bt.Click If 客戶_tb.Text <> "" Then Dim Strs As String = Application.StartupPath If File.Exists(Strs + "\LIVE.txt") Then : File.Delete(Strs + "\LIVE.txt") : End If Dim FilePath As String = Strs + "\LIVE.txt" Try Using fs As IO.FileStream = New IO.FileStream(FilePath, FileMode.Append) Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default) Dim str As String = 客戶_tb.Text sw.WriteLine(str) '插入一整行 sw.Flush() End Using End Using Catch ex As Exception End Try 查詢() : Me.Text = "伺服器外網IP查詢 - 已啟動" If 客戶_tb.Text = "" Or IP_tb.Text = "" Then : Else : ConnOpen() : SQL1 = "SELECT 客戶, IP FROM 客戶IP讀取 WHERE 客戶 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() If dr.Read Then If IP_tb.Text <> dr("IP").ToString Then ConnOpen() : SQL1 = "UPDATE 客戶IP讀取 SET IP = N'" & IP_tb.Text & "' WHERE 客戶 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() ConnOpen() : SQL1 = "UPDATE 伺服器檢測 SET IP = N'" & IP_tb.Text & "' WHERE 名稱 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() End If Else : ConnOpen() : SQL1 = "INSERT INTO 客戶IP讀取 (客戶, IP) VALUES (N'" & 客戶_tb.Text & "', N'" & IP_tb.Text & "')" : CmdSet_For_dr() : End If End If Timer1.Enabled = True End If End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 查詢() : Me.Text = "伺服器外網IP查詢 - 已啟動" If 客戶_tb.Text = "" Or IP_tb.Text = "" Then : Else : ConnOpen() : SQL1 = "SELECT 客戶, IP FROM 客戶IP讀取 WHERE 客戶 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() If dr.Read Then If IP_tb.Text <> dr("IP").ToString Then ConnOpen() : SQL1 = "UPDATE 客戶IP讀取 SET IP = N'" & IP_tb.Text & "' WHERE 客戶 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() ConnOpen() : SQL1 = "UPDATE 伺服器檢測 SET IP = N'" & IP_tb.Text & "' WHERE 名稱 LIKE N'" & 客戶_tb.Text & "'" : CmdSet_For_dr() End If Else : ConnOpen() : SQL1 = "INSERT INTO 客戶IP讀取 (客戶, IP) VALUES (N'" & 客戶_tb.Text & "', N'" & IP_tb.Text & "')" : CmdSet_For_dr() : End If End If End Sub End Class