在数据库中存储和检索二进制文件(fpt)

时间:2012-03-04 12:18:18

标签: c# .net

我必须在数据库中存储指纹模板并检索它。 我必须将列名设置为image或var binary(max)吗? 我尝试了一些网站的一些代码,但没有用。

我从数据库中检索了一个空白文件。 我正在使用sql server 2005在c#.net中执行该项目。 ftp文件大小为1.59kb

提前致谢

//to add new user
        public void AddUser(string name,byte[] pf,int length)
        {

//code to insert file in database
            cn = new SqlConnection(connstring);
            cmd = new SqlCommand("adduser", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", name);
            //to save converted image to variable
            SqlParameter UploadedImage = new SqlParameter("@fp", SqlDbType.Binary, length);
            UploadedImage.Value = pf;
            cmd.Parameters.Add(UploadedImage);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        //retrieve fingerprint from database
        public void FingerPrintRtvl(string uid)
        {

                cn = new SqlConnection(connstring);
                adp = new SqlDataAdapter("fingerprintrtvl", cn);
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                adp.SelectCommand.Parameters.AddWithValue("@uid", Convert.ToInt32(uid));
                DataSet ds = new DataSet("MyImages");
                cn.Open();
                adp.Fill(ds, "MyImages");
                cn.Close();

                byte[] MyData = new byte[57];

                DataRow myRow;
                myRow = ds.Tables["MyImages"].Rows[0];

                MyData = (byte[])myRow["fp"];
                int ArraySize = new int();
                ArraySize = MyData.GetUpperBound(0);
                string temp = System.IO.Path.GetTempPath();
                string fpFile = "D:\\" + "fingerprint.fpt";
                FileStream fs = new FileStream(fpFile, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(MyData, 0, ArraySize);
                fs.Close();

        }

3 个答案:

答案 0 :(得分:1)

感谢各位的指导,我得到了解决方案.....非常感谢。
请确保二进制文件的列类型是数据库中的varbinary(max)


    using system.data.Sqlclient;
    using system.IO;
    using system.Data;

  //function to store the fingerprint in database
  public void AddUser(string name)
    {
        // Read the file and convert it to Byte Array//,byte[] pf,int length
        string filePath = @"D:\10.fpt";
        string filename = Path.GetFileName(filePath);
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        br.Close();
        fs.Close();

        //insert the file into database
       SqlConnection cn = new SqlConnection(connstring);
       SqlCommand cmd = new SqlCommand("adduser", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name;
        cmd.Parameters.Add("@fp", SqlDbType.Binary).Value = bytes;

        cn.Open(); cmd.ExecuteNonQuery();
        cn.Close();


    }

    //retrieve fingerprint from database
    public void FingerPrintRtvl(string uid)
    {

        //retrieving the file from the database
      SqlConnection  cn = new SqlConnection(connstring);
       SqlDataAdapter adp = new SqlDataAdapter("fingerprintrtvl", cn);
        adp.SelectCommand.CommandType = CommandType.StoredProcedure;
        adp.SelectCommand.Parameters.AddWithValue("@uid", Convert.ToInt32(uid));
        DataSet ds = new DataSet("MyImages");
        cn.Open();
        adp.Fill(ds, "MyImages");
        cn.Close();

        //storing the file in byte array
        byte[] MyData = new byte[0];

        DataRow myRow;
        myRow = ds.Tables["MyImages"].Rows[0];

        MyData = (byte[])myRow["fp"];
        int ArraySize = new int();
        ArraySize = MyData.GetUpperBound(0);
        string temp = System.IO.Path.GetTempPath();
        string fpFile = "D:\\" + "Aadhaarfingerprint.fpt";
        //saving the byte array in the local drive
        FileStream fs = new FileStream(fpFile, FileMode.OpenOrCreate, FileAccess.Write);
        fs.Write(MyData, 0, ArraySize);
        fs.Close();

    }

答案 1 :(得分:0)

我为你做了一些研究: 看here

似乎您没有按照正确的方式检索二进制数据,您的参数也应该是SqlDbType.VarBinary

答案 2 :(得分:0)

读取文件的所有字节并将其存储到varbinary(max)列。