[英]Interpreting byte[] in stored procedure
我们通过加密搜索字段并比较这些加密值来搜索一个加密字段。 我需要的不过是能够做的是通入PROC(通过实体框架4)加密值(如代码加密),但也允许空,如果不提供的值。
因此,我需要传递一个byte [],但它也需要接受null ...这是否可能,或者如果没有解决方法,该怎么办? 同样,我打电话通过实体框架的存储过程。
谢谢。
鉴于此存储过程:
create procedure dbo.pConvertBytesToInt
@bytes varbinary(4)
as
select convert(int,@bytes)
go
以下代码将执行它,如果传递的参数为null,则传递NULL:
static int? Bytes2IntViaSQL( byte[] @bytes )
{
int? value ;
const string connectionString = "Data Source=localhost;Initial Catalog=sandbox;Integrated Security=SSPI;" ;
using ( SqlConnection connection = new SqlConnection( connectionString ) )
using ( SqlCommand sql = connection.CreateCommand() )
{
sql.CommandType = CommandType.StoredProcedure ;
sql.CommandText = "dbo.pConvertBytesToInt" ;
SqlParameter p1 = new SqlParameter( "@bytes" , SqlDbType.VarBinary ) ;
if ( @bytes == null ) { p1.Value = System.DBNull.Value ; }
else { p1.Value = @bytes ; }
sql.Parameters.Add( p1 ) ;
connection.Open() ;
object result = sql.ExecuteScalar() ;
value = result is DBNull ? (int?)null : (int?)result ;
connection.Close() ;
}
return value ;
}
这个测试线束
static void Main( string[] args )
{
byte[][] testcases = { new byte[]{0x00,0x00,0x00,0x01,} ,
null ,
new byte[]{0x7F,0xFF,0xFF,0xFF,} ,
} ;
foreach ( byte[] bytes in testcases )
{
int? x = Bytes2IntViaSQL( bytes ) ;
if ( x.HasValue ) Console.WriteLine( "X is {0}" , x ) ;
else Console.WriteLine( "X is NULL" ) ;
}
return ;
}
产生预期的结果:
X is 1
X is NULL
X is 2147483647
我们最终通过将其作为字符串推送,然后在proc中对其进行解析来使其正常工作。 那行得通。 但是我相信我读过一个代表Binary []数组的Binary对象,它也会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.