0

我下载了一个注册示例代码供我的 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对不起,有点菜鸟。有没有人这样做过?

4

1 回答 1

3

未经测试,但我相信代码应该是这样的。基本上,我们在将指纹数据写入其中后替换 aMemoryStream并将其位置设置回,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 = "";
}   
于 2013-06-27T06:31:45.573 回答