2013-06-27 73 views
0

數據庫我下載的報名示例代碼爲我的設備的DigitalPersona使用。它可能已經註冊並驗證指紋,但問題是它將指紋.fpt文件保存在文件夾中。我想將它保存在數據庫中。

如何直接保存的指紋在使用SDK的DigitalPersona

這是我已經到目前爲止已經試過。

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

什麼不能在此代碼的工作?異常在哪裏? – Smartis

+0

沒有這個代碼的作品。唯一的問題是它需要首先將指紋保存在文件夾中,然後從該文件夾中檢索它並轉換爲字節並將其保存到數據庫中。有沒有其他解決方法直接將其保存到數據庫? :( – bot

+0

我知道我只是需要一點點扭曲,因爲我已經可以將它保存在數據庫中。 – bot

回答

1

沒有測試,但我相信代碼應該是這樣的。基本上,我們替換爲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 = ""; 
} 
+0

謝謝,它的作品就像一個魅力。:) – bot

+0

@mlorbetske爲數據庫列,其中我存儲流,其中數據類型其分區.. –

+0

@mlorbetske什麼是比較SQL中保存的指紋的最佳方法?請參閱http://stackoverflow.com/q/33082211/1005741 – hriziya

相關問題