ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 62|ตอบกลับ: 0

[VB.NET] โค้ดตรวจสอบวันที่หมดอายุการใช้งานของโปรแกรมด้วยการใช้ Google Sheet เป็นที่เก็บข้อมูล

[คัดลอกลิงก์]

319

กระทู้

511

โพสต์

6472

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6472
โพสต์ เมื่อวาน 12:07 | ดูโพสต์ทั้งหมด |โหมดอ่าน




Add References ... ใช้ Nuget Package Manager จะสะดวกกว่า


ข้อมูลตัวอย่างใน Google Sheet ...

โค้ดตรวจสอบวันที่หมดอายุการใช้งานของโปรแกรมด้วยการใช้ Google Sheet เป็นที่เก็บข้อมูล ผ่านทาง Google Visualization API ...

คลิปวิดีโอประกอบคำอธิบายบางส่วน ...

มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports System.Net
  2. '// Download MaterialSkin.2 packages.
  3. '// [url]https://www.nuget.org/packages/MaterialSkin.2/[/url]
  4. Imports MaterialSkin
  5. Imports Newtonsoft.Json.Linq

  6. '// คลิปวิดีโอการรับไฟล์ Credentials เพื่อทำการติดต่อกับ Google Sheets และการกำหนดสิทธิ์การเข้าถึง
  7. '// [url]https://www.youtube.com/watch?v=xdYsctNAGEE[/url]

  8. '// ------------------------------------------------------------------------------------------------
  9. '// Google Visualization API Reference ... [url]https://developers.google.com/chart/interactive/docs/reference[/url]
  10. '// Google Visualization API
  11. '// เป็น API จาก Google ที่ช่วยให้เราสามารถดึงข้อมูลจากแหล่งข้อมูลต่างๆ เช่น Google Sheets หรือไฟล์ข้อมูลอื่นๆ
  12. '// แล้วนำข้อมูลนั้นมา แสดงผลในรูปแบบกราฟ, ตาราง หรือ Visualization ต่างๆบนเว็บได้ง่ายๆ
  13. '// เหมาะสำหรับการสร้างแดชบอร์ด หรือรายงานแบบ Interactive ที่ทำงานบนเว็บ
  14. '// Google Visualization API มีฟอร์แมตข้อมูลเฉพาะที่เรียกว่า DataTable (เหมือนตารางฐานข้อมูล มีคอลัมน์และแถว)
  15. '// เราสามารถดึงข้อมูลผ่าน URL โดยใช้ Google Visualization Query Language (GVQL) ซึ่งคล้าย SQL
  16. '// ------------------------------------------------------------------------------------------------

  17. Public Class frmLogin
  18.     '// ป้อน Spreadsheet ID และชื่อชีต
  19.     Private Const SpreadsheetId As String = "SPREAD_SHEET_ID" '// <-- เปลี่ยนเป็นของคุณ
  20.     'Private Const ApplicationName As String = ""   '// ไม่ได้ใช้งาน
  21.     Private Const SheetName As String = "SHEET_NAME"

  22.     Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  23.         '// ข้อมูลตัวอย่าง
  24.         txtUserName.Text = "admin"
  25.         txtPassword.Text = "admin"
  26.         With Me
  27.             .MinimumSize = New Point(451, 503)
  28.             .MaximumSize = New Point(451, 503)
  29.         End With
  30.         '// Code sample.
  31.         '// [url]https://www.nuget.org/packages/MaterialSkin.2/[/url]
  32.         Dim SkinManager As MaterialSkinManager = MaterialSkinManager.Instance
  33.         SkinManager.AddFormToManage(Me)
  34.         SkinManager.Theme = MaterialSkinManager.Themes.DARK
  35.         SkinManager.ColorScheme = New ColorScheme(Primary.Amber500, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE)

  36.         '// ตัวอย่างของ ColorScheme
  37.         'SkinManager.ColorScheme = New ColorScheme(Primary.Blue300, Primary.Blue500, Primary.Blue500, Accent.Blue400, TextShade.WHITE)
  38.         'SkinManager.ColorScheme = New ColorScheme(Primary.Green600, Primary.Green700, Primary.Green200, Accent.Red100, TextShade.WHITE)
  39.         'SkinManager.ColorScheme = New ColorScheme(Primary.LightBlue600, Primary.LightBlue700, Primary.Green200, Accent.LightGreen700, TextShade.WHITE)
  40.         'SkinManager.ColorScheme = New ColorScheme(Primary.Cyan500, Primary.Cyan700, Primary.Cyan100, Accent.Blue100, TextShade.WHITE)
  41.     End Sub

  42.     Private Sub swPassword_CheckedChanged(sender As Object, e As EventArgs) Handles swPassword.CheckedChanged
  43.         txtPassword.Focus()
  44.         txtPassword.Password = Not swPassword.Checked
  45.     End Sub

  46.     ' / ------------------------------------------------------------------------------------------------
  47.     ' / เริ่มต้นการเข้าสู่ระบบ
  48.     ' / ------------------------------------------------------------------------------------------------
  49.     Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
  50.         Dim UserName As String = txtUserName.Text.Trim()
  51.         Dim Password As String = txtPassword.Text.Trim()
  52.         If String.IsNullOrEmpty(UserName) OrElse String.IsNullOrEmpty(Password) Then
  53.             MessageBox.Show("กรุณากรอก Username และ Password", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  54.             Return
  55.         End If
  56.         '//
  57.         '// ใช้ Google Visualization API เพื่อดึงข้อมูลทั้งหมด
  58.         Dim SheetUrl As String = "https://docs.google.com/spreadsheets/d/" & SpreadsheetId & "/gviz/tq?tqx=out:json&sheet=" & SheetName
  59.         Try
  60.             Using Client As New WebClient()
  61.                 Dim JsonRaw As String = Client.DownloadString(SheetUrl)
  62.                 '// ตัดส่วนหัวออก (Google ใส่ "/*O_o*/" และ "google.visualization.Query.setResponse(...)")
  63.                 Dim startIndex = JsonRaw.IndexOf("(") + 1
  64.                 Dim endIndex = JsonRaw.LastIndexOf(")")
  65.                 Dim jsonData = JsonRaw.Substring(startIndex, endIndex - startIndex)
  66.                 '//
  67.                 Dim jObj As JObject = JObject.Parse(jsonData)
  68.                 Dim rows As JArray = jObj("table")("rows")
  69.                 '// ตัวอย่าง ... Columns ประกอบด้วย {UserPK, UserName, Password, ExpireDate}
  70.                 '// {
  71.                 '//  "table" {
  72.                 '//    "rows": [
  73.                 '//      { "c": [ {"v": "1"}, {"v": "admin"}, {"v": "admin"}, {"v": "30/05/2568"} ] },
  74.                 '//      { "c": [ {"v": "2"}, {"v": "user"}, {"v": "user"}, {"v": "21/05/2568"} ] }
  75.                 '//    ]
  76.                 '//  }
  77.                 '// }
  78.                 For Each row As JObject In rows
  79.                     Dim cols As JArray = row("c")
  80.                     Dim userPK = cols(0)("v").ToString()
  81.                     Dim user = cols(1)("v").ToString()
  82.                     Dim pass = cols(2)("v").ToString()
  83.                     Dim expireDate = cols(3)("v").ToString()
  84.                     '// จัดรูปแบบวันที่ก่อน
  85.                     Dim ExpireDateTime As DateTime? = ParseDateFunctionString(expireDate)
  86.                     If user.Equals(UserName, StringComparison.OrdinalIgnoreCase) AndAlso pass.Equals(Password) Then
  87.                         If ExpireDateTime >= DateTime.Now Then
  88.                             MessageBox.Show("เข้าสู่ระบบสำเร็จ!", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  89.                             frmMain.Show()
  90.                             Me.Hide()
  91.                             Return
  92.                         Else
  93.                             MessageBox.Show("บัญชีหมดอายุ กรุณาติดต่อผู้ดูแลระบบ", "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
  94.                             Application.Exit()
  95.                             Return
  96.                         End If
  97.                     End If
  98.                 Next
  99.                 MessageBox.Show("Username หรือ Password ไม่ถูกต้อง!!!", "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  100.             End Using
  101.         Catch ex As Exception
  102.             MessageBox.Show("เกิดข้อผิดพลาด: " & ex.Message, "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  103.         End Try
  104.     End Sub

  105.     ' / ------------------------------------------------------------------------------------------------
  106.     ' / ต้องเขียนโค้ดดึงค่าปี เดือน วัน ออกจาก string "Date(2568,5,30)" ก่อน
  107.     ' / Google Sheet นับเดือน 1 เป็น 0 เราจึงต้อง +1 เข้าไป
  108.     ' / ------------------------------------------------------------------------------------------------
  109.     Function ParseDateFunctionString(rawDate As String) As DateTime?
  110.         '// ตรวจสอบรูปแบบ "Date(2568,5,30)"
  111.         Dim pattern As String = "^Date\((\d+),(\d+),(\d+)\)[        DISCUZ_CODE_0        ]quot;
  112.         Dim m As Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(rawDate, pattern)
  113.         If m.Success Then
  114.             Dim yearBE As Integer = Integer.Parse(m.Groups(1).Value)
  115.             Dim month As Integer = Integer.Parse(m.Groups(2).Value) + 1 '// เพราะ Google เริ่มนับเดือนที่ 0 เลยต้อง +1 เข้าไป
  116.             Dim day As Integer = Integer.Parse(m.Groups(3).Value)
  117.             '// แปลงปี พ.ศ. เป็น ค.ศ.
  118.             Dim yearAD As Integer = yearBE
  119.             If yearBE > 2500 Then yearAD = yearBE - 543
  120.             Try
  121.                 Dim MyDateTime As New DateTime(yearAD, month, day)
  122.                 Return MyDateTime
  123.             Catch ex As Exception
  124.                 Return Nothing
  125.             End Try
  126.         Else
  127.             Return Nothing
  128.         End If
  129.     End Function

  130.     Private Sub txtUserName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUserName.KeyPress
  131.         '// Press ENTER
  132.         If Asc(e.KeyChar) = 13 Then
  133.             e.Handled = True    '// No beep
  134.             SendKeys.Send("{TAB}")
  135.         End If
  136.     End Sub

  137.     Private Sub txtPassword_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPassword.KeyPress
  138.         '// Press ENTER
  139.         If Asc(e.KeyChar) = 13 Then
  140.             e.Handled = True    '// No beep
  141.             SendKeys.Send("{TAB}")
  142.         End If
  143.     End Sub

  144.     Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
  145.         Me.Close()
  146.     End Sub

  147.     Private Sub frmLogin_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
  148.         Me.Dispose()
  149.         GC.SuppressFinalize(Me)
  150.         Application.Exit()
  151.     End Sub
  152. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET & .Net Framework 4.6.2+ ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2025-5-25 20:51 , Processed in 0.217349 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้