如何将生物特征模板直接保存到数据库 (VB.Net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23752625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save Biometric Template directly into Database (VB.Net)
提问by Owell
I already managed to get the biometric template data but unfortunately I cannot save or insert it directly into the Database. I cannot convert the template into bytes and dump it to table. This is my code:
我已经设法获取了生物识别模板数据,但不幸的是我无法将其直接保存或插入到数据库中。我无法将模板转换为字节并将其转储到表中。这是我的代码:
Private Sub btnSaveFinger1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveFinger1.Click
Try
Dim sql As String
sql = "INSERT INTO [Kamog_HR].[dbo].[T_FingerPrintMaster] " & _
"([fpm_EmployeeID] " & _
",[fpm_FingerIndex] " & _
",[fpm_FingerPrintTemplate] " & _
",[Update_By] " & _
",[Created_Date]) " & _
"VALUES " & _
"('" & txtEmpID.Text & "' " & _
",0 " & _
",@template " & _
",'SA' " & _
",'" & Now & "') "
OpenConnection()
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
ExecuteSaveFingerPrint(sql, str)
Catch ex As Exception
SetPrompt("Cannot save the fingerprint template")
Finally
CloseConnection()
End Try
End Sub
Public Sub ExecuteSaveFingerPrint(ByVal sql As String, ByVal str As MemoryStream)
Dim cmd As New SqlCommand(sql, con)
cmd.CommandTimeout = 0
Dim serializedTemplate As Byte() = str.ToArray()
Dim paramater As New SqlParameter("@template", serializedTemplate)
cmd.Parameters.Add(paramater)
cmd.ExecuteNonQuery()
End Sub
During cmd.ExecuteNonQueryit return an error messagesaying
在cmd.ExecuteNonQuery它返回一个错误消息说
"Conversion failed when converting date and/or time from character string."
Even if I remove the Created_Dateinto the SQL script same error appears. I don't know why and how can I save or insert the fingerprint template directly into the Database.
即使我将其删除Created_Date到 SQL 脚本中也会出现同样的错误。我不知道为什么以及如何将指纹模板直接保存或插入到数据库中。
采纳答案by Owell
I already solved my own problem. This is the sample code of how did I insert the biometric template into the database:
我已经解决了我自己的问题。这是我如何将生物识别模板插入数据库的示例代码:
Private Sub btnSaveFinger1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveFinger1.Click
Try
Dim sql As String
If verifyFingerPrint() = True Then
sql = ""
Else
sql = "INSERT INTO [Kamog_HR].[dbo].[T_FingerPrintMaster] " & _
"([fpm_EmployeeID] " & _
",[fpm_FingerIndex] " & _
",[fpm_FingerPrintTemplate] " & _
",[Update_By] " & _
",[Created_Date]) " & _
"VALUES " & _
"('" & txtEmpID.Text & "' " & _
"," & GetFingerID() & " " & _
",@fptemp " & _
",'SA' " & _
",'" & Now & "') "
End If
OpenConnection()
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
ExecuteSaveFingerPrint(sql, str)
MsgBox("Fingerprint Successfully Saved!", vbInformation, "Fingerprint")
Catch ex As Exception
MsgBox("Cannot save the fingerprint template. Please try again")
pbFinger1.Image = Nothing
pbFinger2.Image = Nothing
pbFinger3.Image = Nothing
pbFinger4.Image = Nothing
FingerIndex = 4
Enroller.Clear()
StopCapture()
OnTemplate(Nothing)
StartCapture()
Finally
CloseConnection()
End Try
End Sub
Public Sub ExecuteSaveFingerPrint(ByVal sql As String, ByVal str As MemoryStream)
Dim cmd As New SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
Dim serializedTemplate As Byte() = str.ToArray()
Dim paramater As New SqlParameter("@fptemp", serializedTemplate)
cmd.Parameters.Add(paramater)
cmd.ExecuteNonQuery()
End Sub
Thank you guys!
谢谢你们!

