如何将生物识别模板直接保存到数据库中(VB.Net)

时间:2014-05-20 06:40:55

标签: sql sql-server vb.net fingerprint biometrics

我已经设法获取生物识别模板数据,但遗憾的是我无法将其直接保存或插入数据库。我无法将模板转换为字节并将其转储到表中。这是我的代码:

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

cmd.ExecuteNonQuery期间,它会返回错误消息,表示

"Conversion failed when converting date and/or time from character string."

即使我将Created_Date删除到SQL脚本中,也会出现相同的错误。我不知道为什么以及如何将指纹模板直接保存或插入数据库。

1 个答案:

答案 0 :(得分:0)

我已经解决了自己的问题。这是我如何将生物识别模板插入数据库的示例代码:

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

谢谢你们!