简体   繁体   English

如何在 SQL Server 中存储和检索 varbinary(max) 列

[英]How to Store and Retrieve a varbinary(max) Column in SQL Server

I am developing an application in which I want to store the user's fingerprint into the database and then compare it with the one taken from the device.我正在开发一个应用程序,我想在其中将用户的指纹存储到数据库中,然后将其与从设备中获取的指纹进行比较。 I've been having certain issues while converting a varbinary(max) column back to a byte[].我在将varbinary(max)列转换回 byte[] 时遇到了某些问题。 I have tried to use the GetSqlBinary function but it gives me indexoutofrangeException .我曾尝试使用GetSqlBinary函数,但它给了我indexoutofrangeException

I am using the code below for storing the template into the database but found that the value is the same for all users.我使用下面的代码将模板存储到数据库中,但发现所有用户的值都相同。 (eg 0x000000) (例如 0x000000)

public int insernewVoter(NSubject thumb) 
{
    connectionOpen();
    byteArray = thumb.GetTemplateBuffer().ToArray();
    int insert = 0;

    cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "INSERT INTO VOTER (THUMB) VALUES(CONVERT(varbinary(max),'" + byteArray + "'))";
    int rowsupdated = cmd.ExecuteNonQuery();

    if (rowsupdated <= 0) {
        MessageBox.Show("Ho Gya");
    }
    else {
        MessageBox.Show("AP MAR KYN NAI JATA :D");
    }
    return 0;
    connectionClose();
}

Can anyone please show me how I can insert the byte[] into the varbinary(max) column and then retrieve it?谁能告诉我如何将 byte[] 插入 varbinary(max) 列然后检索它?

You should ALWAYS use parameters.您应该始终使用参数。 Give this a shot:试一试:

using(var conn = new SqlConnection("YOUR CONNECTION STRING ..."))
using (var cmd = new SqlCommand("INSERT INTO VOTER (THUMB) VALUES(@THUMB)", conn)) {
    conn.Open();
    var param = new SqlParameter("@THUMB", SqlDbType.Binary) {
        // here goes your binary data (make sure it's correct)
        Value = thumb.GetTemplateBuffer().ToArray()
    };
    cmd.Parameters.Add(param);
    int rowsAffected = cmd.ExecuteNonQuery();

    // do your other magic ...
}

EDIT编辑

Since you've asked how to retrieve it, you can do something like (not sure of your exact requirements, but it should give you the idea):由于您已经询问了如何检索它,您可以执行以下操作(不确定您的确切要求,但它应该给您这个想法):

private byte[] GetThumbData(int userId) {
    using (var conn = new SqlConnection("YOUR CONNECTION STRING ..."))
    using (var cmd = new SqlCommand("SELECT THUMB FROM VOTER WHERE ID = @ID", conn)) {
        conn.Open();
        cmd.Parameters.AddWithValue("@ID", userId);
        return cmd.ExecuteScalar() as byte[];
    }
}

if u have those finger prints in file format , thn u can use the following code , wch convert pdf into byte and byte again to pdf如果你有文件格式的指纹,那么你可以使用以下代码,将pdf转换为字节,再将字节转换为pdf

  string filepath = Server.MapPath("~/pdf/" + file.FileName);
  byte[] bytes = System.IO.File.ReadAllBytes(filepath);

and pass this to Database field of varbinary now to retrive this content in to pdf u can use并将其传递给 varbinary 的 Database 字段,以将此内容检索为您可以使用的 pdf

 byte[] pdfcontent =  (byte[])DS.Tables[0].Rows[0]["PDFContent"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel Server 2008的varbinary(max)列中插入/检索Excel文件? - How do I insert/retrieve Excel files to varbinary(max) column in SQL Server 2008? 将DataTable存储在SQL的Varbinary列中,然后将Varbinary取回DataTable - Store DataTable in a Varbinary column in SQL and retrieve Varbinary back into DataTable 如何将图像存储到varbinary(max)列? - How to store images to a varbinary(max) column? SQL Server VARBINARY(最大) - SQL Server VARBINARY(MAX) 从SQL Server中检索varbinary(MAX)到C#中的byte [] - Retrieve varbinary(MAX) from SQL Server to byte[] in C# 如何将 Byte[] 转换为数据以在 SQL Server 的 varbinary(max) 列中插入值? - How convert a Byte[] to a data to insert a value in a varbinary(max) column in SQL Server? 如何将文件(PDF)插入到varbinary SQL Server列中,然后检索它? - How do you insert a file (PDF) into a varbinary SQL Server column and later retrieve it? 将存储在SQL XML参数中的byte []存储到SQL Server 2005中的varbinary(MAX)字段。可以完成吗? - Store a byte[] stored in a SQL XML parameter to a varbinary(MAX) field in SQL Server 2005. Can it be done? 如何检索存储为Varbinary(max)的字节数组 - How to retrieve a byte array stored as Varbinary(max) 需要使用C#从SQL Server 2014到Xamarin检索图像(Varbinary MAX) - Need to retrieve images (Varbinary MAX) from SQL Server 2014 to Xamarin by using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM