C# 如何在SQL Server中存储和检索varbinary(max)列
我正在开发一个应用程序,我希望将用户的指纹存储到数据库中,然后将其与从设备中获取的指纹进行比较。 在将C# 如何在SQL Server中存储和检索varbinary(max)列,c#,arrays,sql-server,varbinarymax,C#,Arrays,Sql Server,Varbinarymax,我正在开发一个应用程序,我希望将用户的指纹存储到数据库中,然后将其与从设备中获取的指纹进行比较。 在将varbinary(max)列转换回字节[]时,我遇到了一些问题。我曾尝试使用GetSqlBinary函数,但它给了我indexoutofrangeException 我使用下面的代码将模板存储到数据库中,但发现所有用户的值都相同。(例如0x000000) public int insernewVoter(NSObject thumb) { 连接开放(); byteArray=thumb.Get
varbinary(max)
列转换回字节[]时,我遇到了一些问题。我曾尝试使用GetSqlBinary
函数,但它给了我indexoutofrangeException
我使用下面的代码将模板存储到数据库中,但发现所有用户的值都相同。(例如0x000000)
public int insernewVoter(NSObject thumb)
{
连接开放();
byteArray=thumb.GetTemplateBuffer().ToArray();
int insert=0;
cmd=newsqlcommand();
cmd.Connection=con;
cmd.CommandText=“插入表决器(THUMB)值(CONVERT(varbinary(max),”“+byteArray+”)”;
int rowsupdated=cmd.ExecuteNonQuery();
如果(rowsupdated如果你有这些文件格式的指纹,你可以使用下面的代码,wch将pdf转换为字节,再将字节转换为pdf
string filepath = Server.MapPath("~/pdf/" + file.FileName);
byte[] bytes = System.IO.File.ReadAllBytes(filepath);
并将其传递给varbinary的数据库字段
现在要将此内容检索到pdf,您可以使用
byte[] pdfcontent = (byte[])DS.Tables[0].Rows[0]["PDFContent"];
您应该始终使用参数。尝试一下:
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 ...
}
编辑
既然您已经询问了如何检索它,您可以做一些类似的事情(不确定您的确切需求,但它应该给您提供想法):
是的,但它为所有用户插入了相同的值:?插入后我将如何检索它?非常感谢您,您认真地解决了我的问题:)我大约在4天左右做了这轮fr,我认为有某种sdkissue@Nosheen很高兴我能帮忙:)
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 ...
}
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[];
}
}