อันนี้โค๊ดตัวอย่างในส่วนของค้นหาข้อมูลโดยใช้ MS Access เอามาจากโปรแกรมตัวอย่างครับ
Code (VB.NET)
' อ่านข้อมูลลายนิ้วมือจากฐานข้อมูล
Private Sub AddFinger()
Dim sTemp As String
'Dim strDataSourcePath As String
Dim rsSelect As New ADODB.Recordset
DataBasePath = My.Application.Info.DirectoryPath
Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & "\dbFingerprint.mdb;Persist Security Info=False")
FingerCount = 0
fpcHandle = ZKFPEngX1.CreateFPCacheDB
rsSelect.Open("Select * from us_userinfo", Conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
rsSelect.MoveFirst()
While Not rsSelect.EOF
sTemp = rsSelect.Fields("fpstring").Value
ZKFPEngX1.AddRegTemplateStrToFPCacheDB(fpcHandle, FingerCount, sTemp)
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
If rsSelect.Fields("code").Value <> "" Then
FFingerCode(FingerCount) = rsSelect.Fields("code").Value
Else
FFingerCode(FingerCount) = ""
End If
If rsSelect.Fields("name").Value <> "" Then
FFingerNames(FingerCount) = rsSelect.Fields("name").Value
Else
FFingerNames(FingerCount) = ""
End If
If rsSelect.Fields("pic").Value <> "" Then
FFingerPic(FingerCount) = rsSelect.Fields("pic").Value
Else
FFingerPic(FingerCount) = ""
End If
FingerCount = FingerCount + 1
rsSelect.MoveNext()
End While
rsSelect.Close()
End Sub
ใน FingerPrint.ZIP มันเป้นตัวอย่างทั้งโปรแกรมเลยครับ แต่เป้นของเดิมที่เค้าใช้ MS Access
ผมเอามาแปลงเป็นบันทึกเป็น MySQL แล้ว คีย์รหัสลายนิ้วมือเป็น Fpstring รูปแบบเดียวกับในตัวอย่าง
ส่วนโค๊ดค้นหาเปรียบเทียบผมเห็นมีอยู่แค่อันนี้ครับ เป้นของ MS Access ผมทำให้มันเป้น MySQL ไม่ถูก
Code (VB.NET)
' อ่านข้อมูลลายนิ้วมือจากฐานข้อมูล
Private Sub AddFinger()
Dim sTemp As String
'Dim strDataSourcePath As String
Dim rsSelect As New ADODB.Recordset
DataBasePath = My.Application.Info.DirectoryPath
Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & "\dbFingerprint.mdb;Persist Security Info=False")
FingerCount = 0
fpcHandle = ZKFPEngX1.CreateFPCacheDB
rsSelect.Open("Select * from us_userinfo", Conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
rsSelect.MoveFirst()
While Not rsSelect.EOF
sTemp = rsSelect.Fields("fpstring").Value
ZKFPEngX1.AddRegTemplateStrToFPCacheDB(fpcHandle, FingerCount, sTemp)
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
If rsSelect.Fields("code").Value <> "" Then
FFingerCode(FingerCount) = rsSelect.Fields("code").Value
Else
FFingerCode(FingerCount) = ""
End If
If rsSelect.Fields("name").Value <> "" Then
FFingerNames(FingerCount) = rsSelect.Fields("name").Value
Else
FFingerNames(FingerCount) = ""
End If
If rsSelect.Fields("pic").Value <> "" Then
FFingerPic(FingerCount) = rsSelect.Fields("pic").Value
Else
FFingerPic(FingerCount) = ""
End If
FingerCount = FingerCount + 1
rsSelect.MoveNext()
End While
rsSelect.Close()
End Sub
' อ่านข้อมูลลายนิ้วมือจากฐานข้อมูล
Private Sub AddFinger()
Dim sTemp As String
'Dim strDataSourcePath As String
Dim rsSelect As New ADODB.Recordset
DataBasePath = My.Application.Info.DirectoryPath
Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & "\dbFingerprint.mdb;Persist Security Info=False")
FingerCount = 0
fpcHandle = ZKFPEngX1.CreateFPCacheDB
rsSelect.Open("Select * from us_userinfo", Conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
rsSelect.MoveFirst()
While Not rsSelect.EOF
sTemp = rsSelect.Fields("fpstring").Value
ZKFPEngX1.AddRegTemplateStrToFPCacheDB(fpcHandle, FingerCount, sTemp)
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
If rsSelect.Fields("code").Value <> "" Then
FFingerCode(FingerCount) = rsSelect.Fields("code").Value
Else
FFingerCode(FingerCount) = ""
End If
If rsSelect.Fields("name").Value <> "" Then
FFingerNames(FingerCount) = rsSelect.Fields("name").Value
Else
FFingerNames(FingerCount) = ""
End If
If rsSelect.Fields("pic").Value <> "" Then
FFingerPic(FingerCount) = rsSelect.Fields("pic").Value
Else
FFingerPic(FingerCount) = ""
End If
FingerCount = FingerCount + 1
rsSelect.MoveNext()
End While
rsSelect.Close()
End Sub
Public Class Form1
Dim con As MySql.Data.MySqlClient.MySqlConnection
Dim cmd As MySql.Data.MySqlClient.MySqlCommand
Dim rsSelect As MySql.Data.MySqlClient.MySqlDataReader
Dim sTemp As String
Dim StatusAdd As Integer
Dim FTempLen As Integer
Dim FRegTemplate As Object
Dim FingerCount As Long
Dim fpcHandle As Long
Dim FFingerCode() As String 'รหัส
Dim FFingerNames() As String 'ชื่อ
Dim FFingerPic() As String 'รูป
Dim FMatchType As Integer
Dim ItemFinger As String 'ลายนิ้ว
Dim ItemPathNoPic As String 'ไม่มีรูป
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
con = New MySql.Data.MySqlClient.MySqlConnection(
"server=localhost;user id=test;Password=test;database=test;persist security info=False")
cmd = New MySql.Data.MySqlClient.MySqlCommand()
cmd.Connection = con 'this.msqlConnection;
cmd.CommandText = "SELECT * FROM us_userinfo"
Try
FingerCount = 0
'fpcHandle = ZKFPEngX1.CreateFPCacheDB
con.Open()
rsSelect = cmd.ExecuteReader()
While rsSelect.Read()
sTemp = rsSelect.GetString("fpstring")
'ZKFPEngX1.AddRegTemplateStrToFPCacheDB(fpcHandle, FingerCount, sTemp)
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
If rsSelect.GetString("code") <> "" Then
FFingerCode(FingerCount) = rsSelect.GetString("code")
Else
FFingerCode(FingerCount) = ""
End If
If rsSelect.GetString("name") <> "" Then
FFingerNames(FingerCount) = rsSelect.GetString("name")
Else
FFingerNames(FingerCount) = ""
End If
If rsSelect.GetString("pic") <> "" Then
FFingerPic(FingerCount) = rsSelect.GetString("pic")
Else
FFingerPic(FingerCount) = ""
End If
FingerCount = FingerCount + 1
End While
Catch er As Exception
'do something with the exception
Finally
con.Close()
End Try
End Sub
End Class
Public Class Form1
Dim con As MySql.Data.MySqlClient.MySqlConnection
Dim cmd As MySql.Data.MySqlClient.MySqlCommand
Dim rsSelect As MySql.Data.MySqlClient.MySqlDataReader
Dim sTemp As String
Dim StatusAdd As Integer
Dim FTempLen As Integer
Dim FRegTemplate As Object
Dim FingerCount As Long
Dim fpcHandle As Long
Dim FFingerCode() As String 'รหัส
Dim FFingerNames() As String 'ชื่อ
Dim FFingerPic() As String 'รูป
Dim FMatchType As Integer
Dim ItemFinger As String 'ลายนิ้ว
Dim ItemPathNoPic As String 'ไม่มีรูป
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New MySql.Data.MySqlClient.MySqlConnection("server=localhost;user id=root;Password=34260;database=test;persist security info=False")
Option Explicit
Dim tmplist(0 To 11) As String
Private Sub Form_Load()
'ติดต่อเตรื่องสแกนนิ้มมือ
InitialSensor
'สแกน 1:N
Identify
'กำหนดวันที่
Calendar.Value = Date
'เก็บข้อมูลนิ้วจากฐานช้อมูลเช้าเครื่องสแกน
AddFinger
'ไม่มีรูป
ItemPathNoPic = App.Path & "\pic\00000.jpg"
End Sub
'เก็บข้อมูลนิ้วจากฐานช้อมูลเช้าเครื่องสแกน
Private Sub AddFinger()
Dim sTemp As String
Dim strDataSourcePath As String
Dim rsSelect As New ADODB.Recordset
DataBasePath = App.Path
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & "\dbFingerprint.mdb;Persist Security Info=False"
FingerCount = 0
fpcHandle = ZKFPEngX1.CreateFPCacheDB
rsSelect.Open "Select * from us_userinfo", Conn, adOpenKeyset, adLockOptimistic
rsSelect.MoveFirst
While Not rsSelect.EOF
sTemp = rsSelect.Fields("fpstring")
ZKFPEngX1.AddRegTemplateStrToFPCacheDB fpcHandle, FingerCount, sTemp
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
If rsSelect.Fields("code").Value <> "" Then
FFingerCode(FingerCount) = rsSelect.Fields("code").Value
Else
FFingerCode(FingerCount) = ""
End If
If rsSelect.Fields("name").Value <> "" Then
FFingerNames(FingerCount) = rsSelect.Fields("name").Value
Else
FFingerNames(FingerCount) = ""
End If
If rsSelect.Fields("pic").Value <> "" Then
FFingerPic(FingerCount) = rsSelect.Fields("pic").Value
Else
FFingerPic(FingerCount) = ""
End If
FingerCount = FingerCount + 1
rsSelect.MoveNext
Wend
rsSelect.Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
ZKFPEngX1.FreeFPCacheDB (fpcHandle)
ZKFPEngX1.EndEngine
End Sub
Private Sub LabelExit_Click()
Unload Me
End Sub
Private Sub AddRecord(list As ListBox, newRec As String)
Dim i As Integer
If list.ListCount = 12 Then
list.Clear
End If
list.AddItem newRec, 0
End Sub
Private Sub LabelSetup_Click()
frmLogin.Show vbModal
End Sub
Private Sub Timer1_Timer()
lblTime.Caption = Format(Now, "hh:mm:ss")
End Sub
Private Sub Timer2_Timer()
lblname.Caption = ""
Img.Picture = LoadPicture("")
End Sub
'ค้นหาผลการสแกน
Private Sub ZKFPEngX1_OnCapture(ByVal ActionResult As Boolean, ByVal ATemplate As Variant)
Dim fi As Long, i As Long
Dim Score As Long, ProcessNum As Long
Dim newRec As String
Dim txtfile As String
Dim txtdata As String
If FMatchType = 2 Then
Score = 8
fi = ZKFPEngX1.IdentificationInFPCacheDB(fpcHandle, ATemplate, Score, ProcessNum)
If fi = -1 Then
lblname.Caption = "ไม่พบข้อมูล กรุณาลองใหม่อีกครั้ง"
Img.Picture = LoadPicture("")
Else
lblname.Caption = FFingerCode(fi) & " : " & FFingerNames(fi)
If FFingerPic(fi) = "" Then
FFingerPic(fi) = ItemPathNoPic
End If
Img.Picture = LoadPicture(FFingerPic(fi))
newRec = " " & FFingerCode(fi) & " " & FFingerNames(fi) & " " & Format(Now, "dd-MM-yyyy hh:mm:ss")
AddRecord timelist, newRec
'ตรวจสอบไฟล์เก็บข้อมูล
txtdata = FFingerCode(fi) & Format(Now, "hh:mm")
ChaekFileName Date, txtfile
AddDataToFile txtfile, txtdata
End If
End If
End Sub
Private Sub ZKFPEngX1_OnFeatureInfo(ByVal AQuality As Long)
Dim sTemp As String
If StatusAdd <> 0 Then
If Trim(frmUser.txtid.Text) = "" Then
MsgBox "กรุณาระบุรหัสพนักงานก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
frmUser.txtid.SetFocus
Exit Sub
End If
If Trim(frmUser.txtname.Text) = "" Then
MsgBox "กรุณาระบุชื่อพนักงานก่อนครับ ! ", vbOKOnly + vbCritical, "พบข้อผิดพลาด"
frmUser.txtname.SetFocus
Exit Sub
End If
If frmUser.StatusBar <> "กรุณาคลิกปุ่ม Save" Then
sTemp = ""
If ZKFPEngX1.IsRegister Then
sTemp = "Ststus of enrollment : " & ZKFPEngX1.EnrollIndex & " fingerprint(s) reserved!"
End If
sTemp = sTemp & "Fingerprint quality"
If AQuality <> 0 Then
sTemp = sTemp & " ไม่ผ่าน " & AQuality
Else
sTemp = sTemp & " ผ่าน"
End If
frmUser.StatusBar.Caption = sTemp
End If
End If
End Sub
Private Sub ZKFPEngX1_OnEnroll(ByVal ActionResult As Boolean, ByVal ATemplate As Variant)
If StatusAdd <> 0 Then
If Trim(frmUser.txtid.Text) = "" Then
MsgBox "กรุณาระบุรหัสพนักงานก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
frmUser.txtid.SetFocus
Exit Sub
End If
If Trim(frmUser.txtname.Text) = "" Then
MsgBox "กรุณาระบุชื่อพนักงานก่อนครับ ! ", vbOKOnly + vbCritical, "พบข้อผิดพลาด"
frmUser.txtname.SetFocus
Exit Sub
End If
If Not ActionResult Then
frmUser.StatusBar = "บันทึกผิดพลาด"
Else
frmUser.StatusBar = "กรุณาคลิกปุ่ม Save"
ItemFinger = ZKFPEngX1.GetTemplateAsString
End If
End If
End Sub
Code (VB.NET)
Option Explicit
Dim ItemIDOld As String
Dim StateClick As String
Dim ItemUserId As String
Dim ItemUserName As String
Dim ItemUserPic As String
'ล็อก Comments
Private Sub lockctrl()
txtid.Locked = True
txtname.Locked = True
Picture1.Visible = True
Picture2.Visible = True
Picture3.Visible = True
Picture4.Visible = False
Picture5.Visible = False
txtid.BackColor = &H80000000
txtname.BackColor = &H80000000
cmdpic.Enabled = False
cmddeletepic.Enabled = False
mfg.Enabled = True
End Sub
'ปลดล็อก Comments
Private Sub UnLockCtrl()
txtid.Locked = False
txtname.Locked = False
Picture1.Visible = False
Picture2.Visible = False
Picture3.Visible = False
Picture4.Visible = True
Picture5.Visible = True
cmdpic.Enabled = True
cmddeletepic.Enabled = True
txtid.BackColor = &HFFFFFF
txtname.BackColor = &HFFFFFF
mfg.Enabled = False
End Sub
Private Sub cmdAdd_Click()
If frmScan.ZKFPEngX1.Active = True Then
txtid.SetFocus
txtid.Text = ""
txtname.Text = ""
StateClick = "Add"
UnLockCtrl
StatusAdd = 1
ItemUserPic = ""
ItemFinger = ""
Img.Picture = LoadPicture(ItemUserPic)
frmScan.ZKFPEngX1.BeginEnroll
StatusBar.Caption = "เริ่มเก็บลายนิ้วมือ"
Else
MsgBox "เกิดข้อผิดพลาดในการเชื่อมต่อ ไม่สามารถเพิ่มผู้ใช้งานได้!", vbOKOnly + vbCritical, "เกิดข้อผิดพลาด"
End If
End Sub
Private Sub cmdCancel_Click()
lockctrl
StatusAdd = 0
End Sub
Private Sub cmdDelete_Click()
If txtid.Text <> "" Then
DeleteUser txtid.Text
Else
MsgBox "กรุณาคลิก เลือกรายการที่จะ DELETE ก่อน ! ", vbOKOnly + vbInformation, "พบข้อผิดพลาด"
End If
End Sub
'ลบข้อมูล
Private Sub DeleteUser(tmpID As String)
On Error GoTo PictureNotFound
Dim comUpdate As New ADODB.Command
Dim sqlUpdate As String
Conn.BeginTrans
With comUpdate
.ActiveConnection = Conn
.CommandType = adCmdText
End With
sqlUpdate = "DELETE * FROM us_userinfo"
sqlUpdate = sqlUpdate & " Where (code='" & tmpID & "')"
With comUpdate
.CommandText = sqlUpdate
.Execute
End With
If MsgBox("คุณต้อง DELETE พนักงานชื่อ " & txtname.Text & " ใช่หรือไม่?", vbYesNo + vbQuestion, "คำยืนยัน") = vbYes Then
Conn.CommitTrans
'แสดงชื่อ
DisplayUser mfg
With mfg
ItemUserId = (.TextMatrix(1, 0))
ItemUserName = (.TextMatrix(1, 1))
ItemUserPic = (.TextMatrix(1, 2))
ItemFinger = (.TextMatrix(1, 3))
End With
txtid.Text = ItemUserId
txtname.Text = ItemUserName
If ItemUserPic = "" Then
Img.Picture = LoadPicture(ItemPathNoPic)
Else
Img.Picture = LoadPicture(ItemUserPic)
End If
Else
Conn.RollbackTrans
End If
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Or (Err.Number = 481) Then
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
Exit Sub
End If
End Sub
'แสดงรายการในกริด
Public Sub DisplayUser(mfgName As MSHFlexGrid)
Dim sqlSelect As String
Dim rsSelect As New ADODB.Recordset
Dim i As Integer
sqlSelect = "SELECT * FROM us_userinfo ORDER BY code"
With rsSelect
If .State = adStateOpen Then .Close
.ActiveConnection = Conn
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.Open sqlSelect
If .RecordCount <> 0 Then
ClearGrid mfgName
With mfgName
For i = 1 To rsSelect.RecordCount
.TextMatrix(i, 0) = rsSelect.Fields("code").Value
.TextMatrix(i, 1) = rsSelect.Fields("name").Value
If rsSelect.Fields("pic").Value <> "" Then
.TextMatrix(i, 2) = rsSelect.Fields("pic").Value
Else
.TextMatrix(i, 2) = ""
End If
If rsSelect.Fields("fpstring").Value <> "" Then
.TextMatrix(i, 3) = rsSelect.Fields("fpstring").Value
Else
.TextMatrix(i, 3) = ""
End If
rsSelect.MoveNext
.Rows = .Rows + 1
If .Rows > rsSelect.RecordCount Then
.Rows = rsSelect.RecordCount + 1
End If
Next
End With
Else
ClearGrid mfgName
End If
Setmfg mfgName
End With
rsSelect.Close
Set rsSelect = Nothing
End Sub
Private Sub cmddeletepic_Click()
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
End Sub
Private Sub cmdEdit_Click()
If frmScan.ZKFPEngX1.Active = True Then
StatusAdd = 1
If txtid.Text <> "" Then
Call UnLockCtrl
ItemIDOld = txtid.Text
StateClick = "Edit"
frmScan.ZKFPEngX1.BeginEnroll
StatusBar.Caption = "เริ่มเก็บลายนิ้วมือ"
Else
MsgBox "กรุณาคลิก รายการที่จะ EDIT ก่อน ! ", vbOKOnly + vbInformation, "พบข้อผิดพลาด"
End If
Else
MsgBox "เกิดข้อผิดพลาดในการเชื่อมต่อ ไม่สามารถแก้ไขผู้ใช้งานได้!", vbOKOnly + vbCritical, "เกิดข้อผิดพลาด"
End If
End Sub
Private Sub cmdpic_Click()
On Error GoTo PictureNotFound
With cdlBrowse
.DialogTitle = "เลือกรูปภาพ"
.CancelError = False
.Action = 1
ItemUserPic = .Filename
End With
If ItemUserPic <> "" Then
Img.Picture = LoadPicture(ItemUserPic)
End If
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Or (Err.Number = 481) Then
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
Exit Sub
End If
End Sub
Private Sub cmdSave_Click()
If StateClick = "Edit" Then
'กรณีแก้ไข
Call UpdateData
Else
'กรณีเพิ่มใหม่
Call AddData
End If
End Sub
Private Sub Form_Load()
On Error GoTo PictureNotFound
If frmScan.ZKFPEngX1.Active = True Then
FMatchType = 0
DisplayUser mfg
lockctrl
If frmScan.ZKFPEngX1.Active = True Then
StatusBar.Caption = "เชื่อมต่อสำเร็จ"
Else
StatusBar.Caption = "การเชื่อมต่อ เกิดข้อผิดพลาด"
End If
With mfg
ItemUserId = (.TextMatrix(1, 0))
ItemUserName = (.TextMatrix(1, 1))
ItemUserPic = (.TextMatrix(1, 2))
ItemFinger = (.TextMatrix(1, 3))
End With
txtid.Text = ItemUserId
txtname.Text = ItemUserName
If ItemUserPic = "" Then
Img.Picture = LoadPicture(ItemPathNoPic)
Else
Img.Picture = LoadPicture(ItemUserPic)
End If
Else
MsgBox "เกิดข้อผิดพลาดในการเชื่อมต่อ กรุณาปิดโปรแกรมแล้วเรียกใช้อีกครั้ง!", vbOKOnly + vbCritical, "เกิดข้อผิดพลาด"
Exit Sub
End If
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Or (Err.Number = 481) Then
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
Exit Sub
End If
End Sub
Private Sub UpdateData()
On Error GoTo DuplicateERROR
Dim comUpdate As New ADODB.Command
Dim sqlUpdate As String
Dim tmpSex As String
If Trim(txtid.Text) = "" Then
MsgBox "กรุณาระบุรหัสพนักงานก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
txtid.SetFocus
Exit Sub
End If
If Trim(txtname.Text) = "" Then
MsgBox "กรุณาระบุชื่อพนักงานก่อนครับ ! ", vbOKOnly + vbCritical, "พบข้อผิดพลาด"
txtname.SetFocus
Exit Sub
End If
If (Trim(ItemFinger) = "") Then
MsgBox "กรุณากำหนดลายนิ้วมือก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
Exit Sub
End If
Conn.BeginTrans
With comUpdate
.ActiveConnection = Conn
.CommandType = adCmdText
End With
sqlUpdate = "UPDATE us_userinfo"
sqlUpdate = sqlUpdate & " SET code='" & txtid.Text & "',name='" & txtname.Text & "',"
sqlUpdate = sqlUpdate & " pic='" & ItemUserPic & "',fpstring='" & ItemFinger & "'"
sqlUpdate = sqlUpdate & " Where (code='" & ItemIDOld & "')"
With comUpdate
.CommandText = sqlUpdate
.Execute
End With
If MsgBox("คุณต้องการแก้ไขข้อมูลของพนักงานชื่อ " & txtname.Text & " ใช่หรือไม่?", vbYesNo + vbQuestion, "คำยืนยัน") = vbYes Then
Conn.CommitTrans
ItemIDOld = ""
StatusBar.Caption = ""
'แสดงชื่อพนักงาน
DisplayUser mfg
lockctrl
'เก็บข้อมูลนิ้วจากฐานช้อมูลเช้าเครื่องสแกน
frmScan.ZKFPEngX1.AddRegTemplateStrToFPCacheDB fpcHandle, FingerCount, ItemFinger
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
FFingerCode(FingerCount) = txtid.Text
FFingerNames(FingerCount) = txtname.Text
FFingerPic(FingerCount) = ItemUserPic
FingerCount = FingerCount + 1
Else
Conn.RollbackTrans
lockctrl
End If
StatusAdd = 0
DuplicateERROR:
If Err.Number = -2147467259 Then
MsgBox "คุณกำหนดรหัสซ้ำ !", vbOKOnly + vbCritical, "เกิดข้อผิดพลาด"
Conn.RollbackTrans
txtid.SetFocus
End If
End Sub
Private Sub AddData()
On Error GoTo DuplicateERROR
Dim comUpdate As New ADODB.Command
Dim sqlUpdate As String
Dim tmpSex As String
If Trim(txtid.Text) = "" Then
MsgBox "กรุณาระบุรหัสพนักงานก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
txtid.SetFocus
Exit Sub
End If
If Trim(txtname.Text) = "" Then
MsgBox "กรุณาระบุชื่อพนักงานก่อนครับ ! ", vbOKOnly + vbCritical, "พบข้อผิดพลาด"
txtname.SetFocus
Exit Sub
End If
If (Trim(ItemFinger) = "") Then
MsgBox "กรุณากำหนดลายนิ้วมือก่อนครับ !!!", vbOKOnly + vbInformation, "ผลการตรวจสอบ"
Exit Sub
End If
Conn.BeginTrans
sqlUpdate = "INSERT INTO us_userinfo(code,name,pic,fpstring) "
sqlUpdate = sqlUpdate & " VALUES('" & txtid.Text & "','" & txtname.Text & "','" & ItemUserPic & "','" & ItemFinger & "')"
With comUpdate
.ActiveConnection = Conn
.CommandType = adCmdText
.CommandText = sqlUpdate
.Execute
End With
If MsgBox("คุณต้องการเพิ่มพนักงานใหม่ชื่อ " & txtname.Text & " ใช่หรือไม่?", vbYesNo + vbQuestion, "คำยืนยัน") = vbYes Then
Conn.CommitTrans
'แสดงชื่อพนักงาน
StatusBar.Caption = ""
DisplayUser mfg
lockctrl
'เก็บข้อมูลนิ้วจากฐานช้อมูลเช้าเครื่องสแกน
frmScan.ZKFPEngX1.AddRegTemplateStrToFPCacheDB fpcHandle, FingerCount, ItemFinger
ReDim Preserve FFingerCode(FingerCount + 1)
ReDim Preserve FFingerNames(FingerCount + 1)
ReDim Preserve FFingerPic(FingerCount + 1)
FFingerCode(FingerCount) = txtid.Text
FFingerNames(FingerCount) = txtname.Text
FFingerPic(FingerCount) = ItemUserPic
FingerCount = FingerCount + 1
Else
Conn.RollbackTrans
lockctrl
End If
StatusAdd = 0
DuplicateERROR:
If Err.Number = -2147467259 Then
MsgBox "คุณกำหนดรหัสซ้ำ !", vbOKOnly + vbCritical, "เกิดข้อผิดพลาด"
Conn.RollbackTrans
txtid.SetFocus
End If
End Sub
' set รูปแบบ Grid mfg
Public Sub Setmfg(mfgName As MSHFlexGrid)
Dim i As Integer
With mfgName
.ColWidth(0) = 1200
.ColWidth(1) = 1800
.ColWidth(2) = 0
.ColWidth(3) = 0
.TextMatrix(0, 0) = "รหัสพนักงาน"
.TextMatrix(0, 1) = "ชื่อ-สกุล"
.TextMatrix(0, 2) = "ลิ้งรูปภาพ"
.TextMatrix(0, 3) = "ลายนิ้ว"
End With
With mfgName
For i = 0 To 2
.FixedAlignment(i) = flexAlignLeftCenter ' จัดตำหน่งส่วนหัว
.ColAlignment(0) = flexAlignCenterCenter
.ColAlignment(1) = flexAlignLeftCenter
Next
End With
End Sub
Public Sub ClearGrid(mfgName As MSHFlexGrid)
Dim i As Integer
With mfgName
For i = 1 To .Rows - 2
If .Rows > 2 Then
.Rows = .Rows - 1
End If
Next
.Clear
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
StatusAdd = 0
FMatchType = 2
End Sub
Private Sub mfg_Click()
On Error GoTo PictureNotFound
With mfg
ItemUserId = (.TextMatrix(.Row, 0))
ItemUserName = (.TextMatrix(.Row, 1))
ItemUserPic = (.TextMatrix(.Row, 2))
ItemFinger = (.TextMatrix(.Row, 3))
End With
txtid.Text = ItemUserId
txtname.Text = ItemUserName
If ItemUserPic = "" Then
Img.Picture = LoadPicture(ItemPathNoPic)
Else
Img.Picture = LoadPicture(ItemUserPic)
End If
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Or (Err.Number = 481) Then
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
Exit Sub
End If
End Sub
Private Sub mfg_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo PictureNotFound
With mfg
ItemUserId = (.TextMatrix(.Row, 0))
ItemUserName = (.TextMatrix(.Row, 1))
ItemUserPic = (.TextMatrix(.Row, 2))
ItemFinger = (.TextMatrix(.Row, 3))
End With
txtid.Text = ItemUserId
txtname.Text = ItemUserName
If ItemUserPic = "" Then
Img.Picture = LoadPicture(ItemPathNoPic)
Else
Img.Picture = LoadPicture(ItemUserPic)
End If
PictureNotFound:
If (Err.Number = 76) Or (Err.Number = 53) Or (Err.Number = 481) Then
ItemUserPic = ""
Img.Picture = LoadPicture(ItemUserPic)
Exit Sub
End If
End Sub
Code (VB.NET)
Option Explicit
'ติดต่อ DataBase
Public DataBasePath As String
Public Conn As New ADODB.Connection
Public StatusAdd As Integer
Public FTempLen As Integer
Public FRegTemplate As Variant
Public FingerCount As Long
Public fpcHandle As Long
Public FFingerCode() As String 'รหัส
Public FFingerNames() As String 'ชื่อ
Public FFingerPic() As String 'รูป
Public FMatchType As Integer
Public ItemFinger As String 'ลายนิ้ว
Public ItemPathNoPic As String 'ไม่มีรูป
Public Sub InitialSensor()
frmScan.ZKFPEngX1.InitEngine
If frmScan.ZKFPEngX1.Active = True Then
frmScan.ZKFPEngX1.SensorIndex = 1
frmScan.ZKFPEngX1.EnrollCount = 3
frmScan.lblon.Visible = False
frmScan.lbloff.Visible = True
Else
frmScan.lblon.Visible = True
frmScan.lbloff.Visible = False
End If
End Sub
Public Sub Identify()
If frmScan.ZKFPEngX1.IsRegister Then
frmScan.ZKFPEngX1.CancelEnroll
End If
FMatchType = 2
End Sub
' ฟังก์ชั่นเปลี่ยน พ.ศ. ให้เป็น ค.ศ. และรูปแบบวันที่และเวลา
Public Function TimeThaiFormat(EngTime As Date) As Date
Dim tmpYear As String
Dim tmpDate As String
Dim tmpmonth As String
Dim tmptime As String
tmptime = CStr(Format(EngTime, " HH:mm:ss"))
tmpYear = DatePart("yyyy", EngTime)
tmpDate = DatePart("d", EngTime)
If Len(tmpDate) = 1 Then
tmpDate = "0" & tmpDate
Else
tmpDate = tmpDate
End If
tmpmonth = DatePart("m", EngTime)
If Len(tmpmonth) = 1 Then
tmpmonth = "0" & tmpmonth
Else
tmpmonth = tmpmonth
End If
TimeThaiFormat = tmpYear & "-" & tmpmonth & "-" & tmpDate & tmptime
End Function
'ตรวจสอบไฟล์เก็บข้อมูล
Public Function ChaekFileName(tmptime As Date, tmpResult As String)
Dim tmpYear As String
Dim tmpDate As String
Dim tmpmonth As String
tmpYear = DatePart("yyyy", tmptime)
tmpYear = Right(tmpYear, Len(tmpYear) - 2)
tmpDate = DatePart("d", tmptime)
If Len(tmpDate) = 1 Then
tmpDate = "0" & tmpDate
Else
tmpDate = tmpDate
End If
tmpmonth = DatePart("m", tmptime)
If Len(tmpmonth) = 1 Then
tmpmonth = "0" & tmpmonth
Else
tmpmonth = tmpmonth
End If
tmpResult = tmpYear & tmpmonth & tmpDate
End Function
'เช็คว่ามีไฟล์หรือยัง ถ้ามีให้เพิ่มรายการได้
Public Sub AddDataToFile(tmpFile As String, tmpdata As String)
On Error GoTo CreatFile
Dim PathFileini As String
Dim tmpFreeFile As Integer
PathFileini = DataBasePath
If Right$(PathFileini, 1) <> "\" Then
PathFileini = PathFileini & "\data\"
End If
PathFileini = PathFileini & tmpFile & ".txt"
tmpFreeFile = FreeFile
Open PathFileini For Append As #tmpFreeFile
Print #tmpFreeFile, tmpdata
Close
CreatFile:
Select Case Err.Number
Case 53 ' ถ้าไม่มีไฟล์ให้สร้างใหม่
CreateFile tmpFile
End Select
End Sub
Public Sub CreateFile(tmpFile As String)
Dim Filename As String
Dim memFreeFile As String
Dim PathFile As String
memFreeFile = FreeFile
PathFile = DataBasePath
If Right$(PathFile, 1) <> "\" Then
PathFile = PathFile & "\data\"
End If
Filename = "" & PathFile & tmpFile & ".txt"
Open Filename For Append As #memFreeFile
Close
End Sub