我为我的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我很抱歉有点菜鸟。有人曾经这样做过吗?
答案 0 :(得分:2)
未经测试,但我相信代码应如下所示。基本上,我们在将指纹数据写入MemoryStream
之后替换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 = "";
}
,以便可以回读数据并将其格式化以便存储,从而使其余代码保持完整(除了名称更改)
{{1}}