0

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?

bot
  • 4,841
  • 3
  • 42
  • 67
  • What doesn't work on this code? Where is the Exception? – Smartis has left SO again Jun 27 '13 at 06:18
  • No that code works. The only problem is it needs to save first the fingerprint on a folder then I retrieve it from that folder and convert to byte and save it to database. Is there other way around to save it directly to database? :( – bot Jun 27 '13 at 06:23
  • I know I just need a little twist around because I already could save it in the database. – bot Jun 27 '13 at 06:24
  • Does `Template.Serialize(fs);` require that `fs` be a `FileStream` or just a `Stream`? – mlorbetske Jun 27 '13 at 06:25
  • @mlorbetske I dont know I think any way around will do as long as it works because that line of code is already there when I donwloaded the sample program. – bot Jun 27 '13 at 06:29
  • @bot Do you have answer to this question http://stackoverflow.com/q/33082211/1005741 – hriziya Oct 13 '15 at 05:56
  • @HiteshRiziya sorry no bro. – bot Oct 13 '15 at 06:11

1 Answers1

3

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 = "";
}   
mlorbetske
  • 5,529
  • 2
  • 28
  • 40