解释存储过程中的 byte[]

发布于 2024-11-05 06:31:42 字数 179 浏览 1 评论 0原文

我们通过加密搜索字段并比较这些加密值来搜索加密字段。我需要做的是将加密值(因为代码对其进行加密)传递到过程(通过实体框架 4),但如果未提供该值,也允许 null。

所以我需要传递一个 byte[] 但它也需要接受空值...这是否可能,或者如果不可能的话有什么解决方法?再次,我通过实体框架调用存储过程。

谢谢。

A proc we have searches an encrypted field by encrypting the search field and comparing these encrypted values. What I need though to be able to do is to pass into the proc (through Entity Framework 4) the encrypted value (as the code encrypts it), but also allow null if the value is not provided.

So I need to pass in a byte[] but it also needs to accept nulls... is this even possible, or what is a workaround if its not? Again, I'm calling a stored procedure through entity framework.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-11-12 06:31:43

我们最终通过将其作为字符串推送,然后在过程中解析它来使其工作。那行得通。但我相信我读到有一个表示 byte[] 数组的 Binary 对象,这也可以工作。

We ended up getting it to work by pushing it as a string, and then parsing it in the proc. That worked. But I believe I read there is a Binary object that represents the byte[] array, and that would have worked too.

清醇 2024-11-12 06:31:42

给定这个存储过程:

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

Given this stored procedure:

create procedure dbo.pConvertBytesToInt

  @bytes varbinary(4)

as

  select convert(int,@bytes)

go

The following code will execute it, passing NULL if the parameter passed is 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 ;
}

This test harness

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 ;
}

produces the expected results:

X is 1
X is NULL
X is 2147483647
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文