【问题标题】:How to save a fingerprint directly in the database using digitalpersona sdk如何使用 digitalpersona sdk 将指纹直接保存在数据库中
【发布时间】:2013-06-27 06:13:46
【问题描述】:

我下载了一个注册示例代码供我的 digitalPersona 设备使用。它可能已经注册并验证了指纹,但问题是它将其指纹 .fpt 文件保存在一个文件夹中。我想将它保存在数据库中。

这是我到目前为止已经尝试过的。

private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Fingerprint Template File (*.fpt)|*.fpt";
            if (save.ShowDialog() == DialogResult.OK)

                using (FileStream fs = File.Open(save.FileName, FileMode.Create, FileAccess.Write))
                {
                    Template.Serialize(fs);

                    fs.Close();

                //Read the file and convert it to byte array
                string filePath = save.FileName;
                string filename = Path.GetFileName(filePath);
                FileStream fst = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fst);
                Byte[] bytes = br.ReadBytes((Int32)fst.Length);
                br.Close();
                fst.Close();


                //Insert the file into database
                SqlConnection cn = new SqlConnection("Data Source=10.115.5.3; Initial Catalog=EnrollmentSampledb;Integrated Security=SSPI;");
                SqlCommand cmd = new SqlCommand("INSERT INTO tblUser VALUES(@ID_NUMBER, @FIRSTNAME, @LASTNAME, @FINGERPRINT, @DATE_ADDED, @DATE_MODIFIED)", cn);
                cmd.Parameters.Add("ID_NUMBER", SqlDbType.NVarChar).Value = tboxIdNum.Text;
                cmd.Parameters.Add("FIRSTNAME", SqlDbType.NVarChar).Value = tboxFname.Text;
                cmd.Parameters.Add("LASTNAME", SqlDbType.NVarChar).Value = tboxLname.Text;
                cmd.Parameters.Add("FINGERPRINT", SqlDbType.Image).Value = bytes;
                cmd.Parameters.Add("DATE_ADDED", SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("DATE_MODIFIED", SqlDbType.DateTime).Value = DateTime.Now;

                cn.Open(); cmd.ExecuteNonQuery();
                cn.Close();

            }

            tboxIdNum.Text = "";
            tboxFname.Text = "";
            tboxLname.Text = "";
        }   

这个在数据库中保存了一个指纹文件,但它需要先将它保存在一个文件夹中。我想将它直接保存在数据库中,但我有点困惑如何去做。我找不到要保存的文件。 T_T对不起,有点菜鸟。有没有人这样做过?

【问题讨论】:

  • 这段代码有什么问题?异常在哪里?
  • 不,该代码有效。唯一的问题是它需要先将指纹保存在文件夹中,然后我从该文件夹中检索它并转换为字节并将其保存到数据库中。有没有其他方法可以直接保存到数据库? :(
  • 我知道我只需要稍微调整一下,因为我已经可以将它保存在数据库中了。
  • Template.Serialize(fs); 是否要求 fsFileStream 或只是 Stream
  • @mlorbetske 我不知道我认为只要可行,任何方法都可以,因为当我下载示例程序时,那行代码已经存在。

标签: c# asp.net sdk fingerprint


【解决方案1】:

未经测试,但我相信代码应该是这样的。基本上,我们替换了MemoryStream,并在将指纹数据写入其中后将其位置设置回0,以便可以读回数据并格式化存储,保留其余代码(名称除外)改变)

private void SaveButton_Click(object sender, EventArgs e)
{
    MemoryStream fingerprintData = new MemoryStream();
    Template.Serialize(fingerprintData);
    fingerprintData.Position = 0;
    BinaryReader br = new BinaryReader(fingerprintData);
    Byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);

    //Insert the file into database
    SqlConnection cn = new SqlConnection("Data Source=10.115.5.3; Initial Catalog=EnrollmentSampledb;Integrated Security=SSPI;");
    SqlCommand cmd = new SqlCommand("INSERT INTO tblUser VALUES(@ID_NUMBER, @FIRSTNAME, @LASTNAME, @FINGERPRINT, @DATE_ADDED, @DATE_MODIFIED)", cn);
    cmd.Parameters.Add("ID_NUMBER", SqlDbType.NVarChar).Value = tboxIdNum.Text;
    cmd.Parameters.Add("FIRSTNAME", SqlDbType.NVarChar).Value = tboxFname.Text;
    cmd.Parameters.Add("LASTNAME", SqlDbType.NVarChar).Value = tboxLname.Text;
    cmd.Parameters.Add("FINGERPRINT", SqlDbType.Image).Value = bytes;
    cmd.Parameters.Add("DATE_ADDED", SqlDbType.DateTime).Value = DateTime.Now;
    cmd.Parameters.Add("DATE_MODIFIED", SqlDbType.DateTime).Value = DateTime.Now;

    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

    tboxIdNum.Text = "";
    tboxFname.Text = "";
    tboxLname.Text = "";
}   

【讨论】:

  • @mlorbetske 用于我在其中存储流的数据库列,它的数据类型与其兼容..
  • @mlorbetske 在 SQL 中比较保存的指纹的最佳方法是什么?看这里stackoverflow.com/q/33082211/1005741
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多