将1维字节数组转换为varbinary(max) [英] convert the 1-dimension byte array to varbinary(max)

查看:70
本文介绍了将1维字节数组转换为varbinary(max)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在vb.net中将1维字节数组转换为varbinary(max),并且还应在消息框中显示转换后的值。当我尝试这个我得到一维数组不能转换为字符串错误。我该怎么办?



最好的帖子会很好欣赏

解决方案

有几件事情这里:VB.NET没有varbinary(max)的概念 - 这是一个SQL数据类型,而不是.NET,并且根据你的字节内容,它可能或者可能不能作为字符串读取,所以在消息框中显示它可能比你想象的更复杂。



所以我假设你想在varbinary字段中将一个字节数组保存到一个SQL数据库,并告诉你如何转换一个字节数组到一个字符串(无论它有什么好处 - 就人类可读性而言,它可能没有任何实际用途)

输出字节数组到SQL:

  Dim  bytes  as  字节()= File.ReadAllBytes(路径)
使用 con 作为 SqlConnection(strConnect)
con.Open()
使用 com 作为 SqlCommand( INSERT INTO myTable(myColumn)VALUES(@COL ),con)
com.Parameters.AddWithValue( @ COL,bytes)
com.ExecuteNonQuery()
结束 使用
结束 使用



将字节转换为字符串:

  Dim  bytes  As   Byte ()= File.ReadAllBytes(path)
Dim s As String = System.Text.Encoding。 ASCII.GetString(bytes)

根据字节数组中的内容,您可能需要使用与ASCII不同的编码值

I need to convert the 1-dimension byte array to varbinary(max) in vb.net and also the coverted value should be display in message box. When i try this i got "1-dimension array cannot be converted to string" error. what can i do?

Best Post will be Well Appreciated

解决方案

There are a couple of things here: VB.NET has no concept of varbinary(max) - that is an SQL datatype, not .NET, and depending on your byte content it may or may not be readable as a string anyway, so displaying it in a message box may be more complex than you think.

So I will assume you want to save an array of bytes to an SQL database in a varbinary field, and show you how to convert a byte array to a string (for whatever good it will do you - it may not be any real use as far as human readability is concerned)
Output byte array to SQL:

Dim bytes as Byte() = File.ReadAllBytes(path)
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn) VALUES (@COL)", con)
        com.Parameters.AddWithValue("@COL", bytes)
        com.ExecuteNonQuery()
    End Using
End Using


Convert bytes to string:

Dim bytes As Byte() = File.ReadAllBytes(path)
Dim s As String = System.Text.Encoding.ASCII.GetString(bytes)

Depending on what is in your byte array, you might need to use a different Encoding value than ASCII


这篇关于将1维字节数组转换为varbinary(max)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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