How to save a fingerprint directly in the database

2020-06-06 08:11发布

问题:

I downloaded an Enrollment Sample Code for my digitalPersona device to use. It could already register and verify fingerprints but the problem is that it save its fingerprint .fpt file in a folder. I want to save it in the database.

This is what I already tried so far.

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 = "";
        }   

This one saves a fingerprint file in the database but it need first to save it in a folder. I want to save it directly in the database but I'm a little confuse how to do it. I can't locate the file to save. T_T I'm sorry a little bit noob. Has anyone has done this before?

回答1:

Not tested, but I believe the code should look like this. Basically, we substitute a MemoryStream and set its position back to 0 after writing the fingerprint data into it so that the data may be read back and formatted for storage, leaving the rest of the code in tact (excepting for the name change)

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 = "";
}