-1

I am using MySql database and column type is VARBINARY. I am trying to save template into database. Stored successfully but in database it is storing "System.Byte[]" but I want to store the binary data. Can you tell why this is displaying in database? is there in code problem or in database problem?

protected override void Process(DPFP.Sample Sample)
{
    base.Process(Sample);
    DPFP.FeatureSet features = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Enrollment);
    DPFP.Capture.SampleConversion ToByte = new DPFP.Capture.SampleConversion();

    if (features != null) try
        {
            MakeReport("The fingerprint feature set was created.");
            Enroller.AddFeatures(features);
        }
        finally
        {
            UpdateStatus();

            // Check if template has been created.
            switch (Enroller.TemplateStatus)
            {
                case DPFP.Processing.Enrollment.Status.Ready:   // report success and stop capturing
                    {
                        OnTemplate(Enroller.Template);
                        SetPrompt("Click Close, and then click Fingerprint Verification.");
                        Stop();

                        MemoryStream fingerprintData = new MemoryStream();
                        Enroller.Template.Serialize(fingerprintData);
                        fingerprintData.Position = 0;
                        BinaryReader br = new BinaryReader(fingerprintData);
                        byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);


                        try
                        {
                            MySqlConnection conn = new MySqlConnection("server=localhost;username=root;password=root;database=enroll");
                            conn.Open();
                            MySqlCommand cmd = new MySqlCommand();
                            cmd.Connection = conn;
                            cmd.CommandText = "INSERT INTO reg(reg_temp) VALUES('" + bytes + "')";
                            cmd.ExecuteNonQuery();
                            conn.Close();
                            MessageBox.Show("Success!");

                        }
                        catch (Exception e)
                        {
                            MakeReport(e.Message);
                        }
                        break;
                    }

                case DPFP.Processing.Enrollment.Status.Failed:  // report failure and restart capturing
                    {
                        Enroller.Clear();
                        Stop();
                        UpdateStatus();
                        OnTemplate(null);
                        Start();
                        break;
                    }
            }
        }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 1
    Please see https://stackoverflow.com/q/332365/11683 for why you should not do `VALUES('" + bytes + "')"` with any data type. When you fix your code to use a parametrized command with a parameter of type `varbinary`, your problem will go away. – GSerg Oct 27 '18 at 08:49
  • the fingerprint is is converted and stored in an bytearray that why (' "+ bytes +" ') is that converted bytearray – tazeem isl Oct 27 '18 at 09:21
  • Because when you concatenate something into a string, and the thing you are concatenating is not already a string, the compiler first has to call `ToString()` on that thing you are concatenating, and `ToString()` of a byte array returns the type name, which is `"System.Byte[]"`. – GSerg Oct 27 '18 at 09:35
  • but in this pots i have scan a fingerprint and that fingerprint template i want to store in database. how to convert that fingerprint template in bytes or binary data and store in database – tazeem isl Oct 27 '18 at 11:09

1 Answers1

1

You would need to use parameters

using(var cmd = new MySqlCommand("INSERT INTO mssqltable(varbinarycolumn) VALUES (@binaryValue)", conn))
{
    cmd.Parameters.Add("@binaryValue", SqlDbType.VarBinary, 8000).Value = bytes;
    cmd.ExecuteNonQuery();
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • thanks for your fast reply. thank you so much. i am still facing same problem. can you review below in answers i explain more my errors – tazeem isl Oct 27 '18 at 09:28