從C#將byte[]保存到SQL Server數據庫中
sql-server bytearray (2)
Varbinary或CHAR - 將值轉換為十六進制。 我經常使用哈希值這樣做,因為它允許我輕鬆地查看和比較它們(在rintouts上,在開發期間),並且開銷很小。
如何將byte []數組保存到SQL Server數據庫中? 此byte []包含HashAlgorithm值。
需要再次使用該數據以供以後使用。 所以轉換它而不是讓它恢復到原來的狀態,這不是我想要的。
提前致謝!
你應該能寫出這樣的東西:
string queryStmt = "INSERT INTO dbo.YourTable(Content) VALUES(@Content)";
using(SqlConnection _con = new SqlConnection(--your-connection-string-here--))
using(SqlCommand _cmd = new SqlCommand(queryStmt, _con))
{
SqlParameter param = _cmd.Parameters.Add("@Content", SqlDbType.VarBinary);
param.Value = YourByteArrayVariableHere;
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
}
使用Linq-to-SQL,你會寫這樣的東西:
using(YourDataContextHere ctx = new YourDataContextHere())
{
SomeClassOfYours item = new SomeClassOfYours();
item.ByteContent = (your byte content here);
ctx.SomeClassOfYourses.InsertOnSubmit(item);
ctx.SubmitChanges();
}
這會將您的byte[]
插入到SQL Server表中作為字節流的類型為VARBINARY
的列中,您可以稍後再次以1:1讀回。