C# 将字节[]从C保存到SQL Server数据库中#
如何将字节[]数组保存到SQL Server数据库中? 此字节[]包含哈希算法值 这些数据需要再次使用。所以转换它,而不是让它回到原来的状态,不是我想要的C# 将字节[]从C保存到SQL Server数据库中#,c#,sql-server,bytearray,C#,Sql Server,Bytearray,如何将字节[]数组保存到SQL Server数据库中? 此字节[]包含哈希算法值 这些数据需要再次使用。所以转换它,而不是让它回到原来的状态,不是我想要的 提前谢谢 使用VARBINARY您应该能够编写如下内容: string queryStmt = "INSERT INTO dbo.YourTable(Content) VALUES(@Content)"; using(SqlConnection _con = new SqlConnection(--your-connection-strin
提前谢谢 使用
VARBINARY
您应该能够编写如下内容:
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();
}
using(YourDataContextHere ctx = new YourDataContextHere())
{
SomeClassOfYours item = new SomeClassOfYours();
item.ByteContent = (your byte content here);
ctx.SomeClassOfYourses.InsertOnSubmit(item);
ctx.SubmitChanges();
}
using (var con = DapperConnection.Con)
{
string firstValue = "any text...";
string secondValue = "HTML code maybe ?";
// Convert your string into byte array:
byte[] htmlCode = Encoding.ASCII.GetBytes(secondValue);
// Define your insert query and execute it:
con.Execute($@" INSERT INTO [dbo].[YourTableName]
( [firstColumnName], [secondColumnName] )
VALUES
( {firstValue}, COMPRESS(@HTML) )", new { HTML = htmlCode });
}
SELECT [firstColumnName], CONVERT(varchar(MAX), DECOMPRESS([secondColumnName])) FROM [YourTableName];
使用Linq to SQL,您可以编写如下内容:
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();
}
using(YourDataContextHere ctx = new YourDataContextHere())
{
SomeClassOfYours item = new SomeClassOfYours();
item.ByteContent = (your byte content here);
ctx.SomeClassOfYourses.InsertOnSubmit(item);
ctx.SubmitChanges();
}
using (var con = DapperConnection.Con)
{
string firstValue = "any text...";
string secondValue = "HTML code maybe ?";
// Convert your string into byte array:
byte[] htmlCode = Encoding.ASCII.GetBytes(secondValue);
// Define your insert query and execute it:
con.Execute($@" INSERT INTO [dbo].[YourTableName]
( [firstColumnName], [secondColumnName] )
VALUES
( {firstValue}, COMPRESS(@HTML) )", new { HTML = htmlCode });
}
SELECT [firstColumnName], CONVERT(varchar(MAX), DECOMPRESS([secondColumnName])) FROM [YourTableName];
这将把您的
byte[]
作为字节流插入SQL Server表中VARBINARY
类型的Content
列中,稍后您可以将其以1:1的比例读回。VARBINARY或CHAR-值转换为十六进制。我经常使用散列值来执行此操作,因为它允许我轻松查看和比较它们(在打印输出时,在开发过程中),并且开销很小。使用Dapper,您可以像这样轻松保存HTML代码:
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();
}
using(YourDataContextHere ctx = new YourDataContextHere())
{
SomeClassOfYours item = new SomeClassOfYours();
item.ByteContent = (your byte content here);
ctx.SomeClassOfYourses.InsertOnSubmit(item);
ctx.SubmitChanges();
}
using (var con = DapperConnection.Con)
{
string firstValue = "any text...";
string secondValue = "HTML code maybe ?";
// Convert your string into byte array:
byte[] htmlCode = Encoding.ASCII.GetBytes(secondValue);
// Define your insert query and execute it:
con.Execute($@" INSERT INTO [dbo].[YourTableName]
( [firstColumnName], [secondColumnName] )
VALUES
( {firstValue}, COMPRESS(@HTML) )", new { HTML = htmlCode });
}
SELECT [firstColumnName], CONVERT(varchar(MAX), DECOMPRESS([secondColumnName])) FROM [YourTableName];
然后,您可以像这样从数据库中解压缩它:
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();
}
using(YourDataContextHere ctx = new YourDataContextHere())
{
SomeClassOfYours item = new SomeClassOfYours();
item.ByteContent = (your byte content here);
ctx.SomeClassOfYourses.InsertOnSubmit(item);
ctx.SubmitChanges();
}
using (var con = DapperConnection.Con)
{
string firstValue = "any text...";
string secondValue = "HTML code maybe ?";
// Convert your string into byte array:
byte[] htmlCode = Encoding.ASCII.GetBytes(secondValue);
// Define your insert query and execute it:
con.Execute($@" INSERT INTO [dbo].[YourTableName]
( [firstColumnName], [secondColumnName] )
VALUES
( {firstValue}, COMPRESS(@HTML) )", new { HTML = htmlCode });
}
SELECT [firstColumnName], CONVERT(varchar(MAX), DECOMPRESS([secondColumnName])) FROM [YourTableName];
回答得好。但是,根据该值是散列的语句,它可能具有恒定的长度。如果是这样的话,考虑使用二进制长度,而不是ValBoo.@ Sean Reilly:Trime-但是不同的哈希算法也会产生不同的长度哈希值,所以您可能希望使用具有合适的最大长度的ValBoin来容纳所有的哈希值。variations@marc_s我将使用linq。我必须做SqlDbType.VarBinary吗?@Yustme:更新了我的答案,将Linq也包括到了SQL中。是的,无论您以何种方式访问SQL Server表,都需要一个VARBINARY列。在Linq to SQL中,它将在实体类中显示为一个“二进制”列,您可以像设置(并读取)任何其他列一样设置它。@marc_也是如此-但是很可能所有行都是相同的哈希算法。如果(且仅当)是这样,我会坚持我的建议,使用定长二进制而不是varbinary。压缩和解压缩是SQL特性还是简洁的特性?蒂尔:不管怎样,这都很酷。