如何将字节数组插入SQL表? [英] How to insert byte array into SQL table?

查看:848
本文介绍了如何将字节数组插入SQL表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我从Byte数组准备了一个十六进制字符串,并在查询中使用它进行连接。十六进制字符串看起来像0x123456789ABCDEF

Hi,

I Prepared a hex string from the Byte array and using it in the query for concatenation. a hex string will look like 0x123456789ABCDEF

hexStr = "0x1234FFCD5";
sql = "Insert into MembersTable
(col1,col2,col3,col4)"
         + "VALUES('" + _GUID + "','" + _Name + "','" + _Pass + "','"
+ hexStr + "');";



这个过程会起作用

我得到的错误就像


will this process will work
am getting error like

Implicit conversion from data type varchar to binary is not allowed. Use the CONVERT function to run this query

推荐答案

而不是将值连接到SQL语句中,使用SqlParameter [ ^ ]。



声明可以看起来像:

Instead of concatenating the values into your SQL statement, use SqlParameter[^].

The statement could look something like:
sql = "Insert into MembersTable
(col1,col2,col3,col4)"
         + "VALUES(@guid,@name,@pass,@hexstr);";







现在,如果您将参数@hexstr定义为二进制类型(请参阅: http:// msdn。 microsoft.com/en-us/library/system.data.sqldbtype.aspx [ ^ ])你可以分配一个参数的正确二进制值,你不必在数据库端转换它。



如果二进制数据的长度不同,也可以考虑使用varbinary而不是binary很多。




Now if you define the parameter @hexstr to type binary (see: http://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx[^]) you can assign a proper binary value to the parameter and you don't have to convert it at database side.

Also consider using varbinary instead of binary if the length of the binary data varies a lot.


参数化查询可以解决这个问题

Parameterizing the query can solve this problem
hexStr = "0x1234FFCD5";
sql = "Insert into MembersTable(col1,col2,col3,col4) VALUES(@guid, @name, @pass, @hex);";

SqlCommand insertCmd=new SqlCommand(sql, conn); /*Assuming conn is an open SqlConnection*/

insertCmd.Parameters.Add("@guid",_GUID);
insertCmd.Parameters.Add("@name",_Name);
insertCmd.Parameters.Add("@pass",_Pass);
insertCmd.Parameters.Add("@hex",hexStr); 

insetCmd.ExecuteNonQuery();



如果问题仍然存在,请提供完整代码。


If problem persists, please provide the full code.


将文件转换为字节



字节[] Attimg;

使用(FileStream fs = new FileStream(fileURL,FileMode.Open))

{

BinaryReader reader = new BinaryReader(fs);



Attimg = reader.ReadBytes((int) fs.Length);



fs.Close();

}



然后将其保存到数据库中..



qry =插入表(c1,c2,c3)值('+ c1 +','+ c2 +',@ c3);



cmd =新sqlcommand(qry,con);



cmd.parameter.addwithvalue(@ c3,sqldbtype.binary).value = Attimg;



con.open();

cmd。 executenonquery();

con.close();
convert your file into byte

Byte[] Attimg;
using (FileStream fs = new FileStream(fileURL, FileMode.Open))
{
BinaryReader reader = new BinaryReader(fs);

Attimg = reader.ReadBytes((int)fs.Length);

fs.Close();
}

and then save it into database..

qry=insert into table (c1,c2,c3) values ('"+c1+"','"+c2+"',@c3);

cmd=new sqlcommand(qry,con);

cmd.parameter.addwithvalue(@c3,sqldbtype.binary).value=Attimg;

con.open();
cmd.executenonquery();
con.close();


这篇关于如何将字节数组插入SQL表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆