Compare 2 fingerprints using SQL and C #

1

Good day I have an application that I set up on the Internet, and adapt it to my needs, I have only managed to get the data captured, I used Digital Person SDK 1.6.1, I have a fingerprint reader: digital person 4500u.

In my database I store them in the following way:

and in my code I keep them in the following way:

private void button1_Click(object sender, EventArgs e)
        {
            MemoryStream fingerprintData = new MemoryStream();
            Enroller.Template.Serialize(fingerprintData);
            fingerprintData.Position = 0;
            BinaryReader br = new BinaryReader(fingerprintData);
            byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);
            try
            {
                /*if (User.id == 0)
                    User.id = dbinfo.compare(User.Login, User.Pass);
                dact_id = dbinfo.InsertFingSample(btarr);
                dbinfo.InsertFinger(User.id, GetFingNum(), dact_id);*/

                SqlCommand vQuery = new SqlCommand("INSERT INTO Huellas VALUES (CONVERT(varbinary(15),@Imagen))", cadena);

                vQuery.Parameters.AddWithValue("@Imagen", bytes);
                SqlParameter[] param2 = { new SqlParameter("@Imagen", SqlDbType.VarBinary) };

                param2[0].Value = bytes;


                cadena.Open();
                var tres= vQuery.ExecuteNonQuery();
                cadena.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("No" + ex.Message);
            }

        }

And my comparison is as follows:

 private void button1_Click(object sender, EventArgs e)
        {
            MemoryStream fingerprintData = new MemoryStream();
            Enroller.Template.Serialize(fingerprintData);
            fingerprintData.Position = 0;
            BinaryReader br = new BinaryReader(fingerprintData);
            byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);
            try
            {
                /*if (User.id == 0)
                    User.id = dbinfo.compare(User.Login, User.Pass);
                dact_id = dbinfo.InsertFingSample(btarr);
                dbinfo.InsertFinger(User.id, GetFingNum(), dact_id);*/

                SqlCommand vQuery = new SqlCommand("SELECT CONVERT(varbinary(15), Huella,1) as Huellas from Huellas where CONVERT(varchar(40), Huella,1) like '%@Imagen%'", cadena);
                vQuery.Parameters.AddWithValue("@Imagen",bytes);


                cadena.Open();
                var reader = vQuery.ExecuteReader();

                if (reader.HasRows)
                {
                    MessageBox.Show("Ya existe");
                    cadena.Close();

                }
                else
                {
                    MessageBox.Show("No Existe");

                    cadena.Close();

                    try
                    {


                        vQuery = new SqlCommand("INSERT INTO Huellas VALUES (CONVERT(varbinary(15),@Imagen))", cadena);
                        vQuery.Parameters.AddWithValue("@Imagen", bytes);
                        SqlParameter[] param3 = { new SqlParameter("@Imagen", SqlDbType.VarBinary) };

                        param3[0].Value = bytes;


                        cadena.Open();
                        vQuery.ExecuteNonQuery();
                        cadena.Close();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("No" + ex.Message);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("No" + ex.Message);
            }

        }

What happens is that even if I put the same finger it always tells me that it does not exist, but when consulting the database it shows me the same records:

Someone can tell me what I'm wrong with when comparing. Because I guess there's the problem there. Since it continues inserting even though it already exists.

Greetings.

    
asked by Ezequie Lopez 19.09.2018 в 20:57
source

1 answer

0

If you want to know if it exists, you should use ExecuteScalar()

cadena.Open();

string query = @"SELECT COUNT(*) from Huellas 
                    where CONVERT(varchar(40), Huella,1) like '%@Imagen%'";
SqlCommand vQuery = new SqlCommand(query, cadena);
vQuery.Parameters.AddWithValue("@Imagen",bytes);

int cant = Convert.ToInt32(vQuery.ExecuteScalar());

if (cant > 0)
{
    MessageBox.Show("Ya existe");
}
else
{
    MessageBox.Show("No Existe");
}

then validate the number of records that match

    
answered by 19.09.2018 в 21:20