C# 将数据表中的列值从字节[]转换为字符串

C# 将数据表中的列值从字节[]转换为字符串,c#,types,datagridview,casting,datatable,C#,Types,Datagridview,Casting,Datatable,总之,我有一个SQL解析器和编辑器,我打算将其集成到我的应用程序中。当我运行以下查询时 select * from sys.sysprocesses; 返回的列之一是字节[]的类型。然而,当我这样做时,这个列很高兴地被放入一个数据表中 bindingSource.DataSource = result.DataTable; 并尝试在DataGridView中显示数据,我得到了明显的ArgumentException。在这种情况下,将字节[]更改为字符串以显示在数据表中的最佳方式是什么 我可以

总之,我有一个SQL解析器和编辑器,我打算将其集成到我的应用程序中。当我运行以下查询时

select * from sys.sysprocesses;
返回的列之一是
字节[]
的类型。然而,当我这样做时,这个列很高兴地被放入一个
数据表中

bindingSource.DataSource = result.DataTable;
并尝试在
DataGridView
中显示数据,我得到了明显的
ArgumentException
。在这种情况下,将
字节[]
更改为
字符串以显示在
数据表中的最佳方式是什么

我可以循环查看
数据表
,并执行以下操作

foreach(DataColumn col in dataTable.Columns)
    if (col.DataType == typeof(byte[]))
        foreach (DataRow row in dataTable.Rows)
            row[col] = Encoding.ASCII.GetString((byte[])row[col]); 
但这将尝试将
字符串
放入
字节[]
列中,并且不起作用。我可以克隆
数据表
,然后更改类型

DataTable dtCloned = dataTable.Clone();
dtCloned.Columns[0].DataType = typeof(String);
foreach (DataRow row in dataTable.Rows)
    dtCloned.ImportRow(row);
但是我需要一个转换步骤来将
字节[]
转换为十六进制字符串。什么是实现我想要的最好、最好也是最有效的方法


感谢您的时间。

如果您使用的是SQL server 2005或更高版本,您可以使用
master.sys.fn\u varbintohextr
函数在查询中进行转换。
例如:

编辑
或者,您可以将
数据表
包装在一个类中,该类处理如下转换:
(这假设您的网格不依赖于将
DataTable
作为数据源)


这就是我最后做这件事的方式

public static void PostProcessData(ref DataTable dataTable)
{
    // Convert byte[] columns.
    List<DataColumn> colCollRem = new List<DataColumn>();
    List<DataColumn> colCollAdd = new List<DataColumn>();
    foreach(DataColumn col in dataTable.Columns)
        if (col.DataType == typeof(byte[]))
            colCollRem.Add(col);

    // Remove old add new.
    foreach (DataColumn col in colCollRem)
    {
        int tmpOrd = col.Ordinal;
        string colName = String.Format("{0}(Hex)", col.ColumnName);
        DataColumn tmpCol = new DataColumn(colName, typeof(String));
        dataTable.Columns.Add(tmpCol);
        colCollAdd.Add(tmpCol);
        foreach (DataRow row in dataTable.Rows)
            row[tmpCol] = Utilities.ByteArrayToHexString((byte[])row[col]);
        dataTable.Columns.Remove(col);
        string colNameNew = colName.Replace("(Hex)", String.Empty);
        dataTable.Columns[colName].ColumnName = colNameNew;
        dataTable.Columns[colNameNew].SetOrdinal(tmpOrd);
    }
}
publicstaticvoid后处理数据(refdatatable)
{
//转换字节[]列。
List colCollRem=新列表();
List colladd=新列表();
foreach(dataTable.Columns中的DataColumn列)
if(col.DataType==typeof(byte[]))
colCollRem.Add(col);
//删除旧的添加新的。
foreach(colCollRem中的数据列col)
{
int tmpOrd=列序号;
string colName=string.Format(“{0}(十六进制)”,col.ColumnName);
DataColumn tmpCol=新的DataColumn(colName,typeof(String));
dataTable.Columns.Add(tmpCol);
冷压添加(tmpCol);
foreach(dataTable.Rows中的DataRow行)
行[tmpCol]=实用程序.ByteArrayToHexString((字节[])行[col]);
dataTable.Columns.Remove(col);
string colNameNew=colName.Replace(“(十六进制)”,string.Empty);
dataTable.Columns[colName].ColumnName=colNameNew;
dataTable.Columns[colnamew].SetOrdinal(tmpOrd);
}
}
使用此转换

public static string ByteArrayToHexString(byte[] p)
{
    byte b;
    char[] c = new char[p.Length * 2 + 2];
    c[0] = '0'; c[1] = 'x';
    for (int y = 0, x = 2; y < p.Length; ++y, ++x)
    {
        b = ((byte)(p[y] >> 4));
        c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
        b = ((byte)(p[y] & 0xF));
        c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
    }
    return new string(c);
}
公共静态字符串ByteArrayToHexString(字节[]p)
{
字节b;
char[]c=新字符[p.Length*2+2];
c[0]=“0”;c[1]=“x”;
对于(int y=0,x=2;y>4));
c[x]=(字符)(b>9?b+0x37:b+0x30);
b=((字节)(p[y]&0xF));
c[++x]=(字符)(b>9?b+0x37:b+0x30);
}
返回新字符串(c);
}

我希望这对其他人有所帮助。

数据的性质是什么,它以字符串的形式有什么意义?这种特殊的列类型可以转换为十六进制表示。了解这一点很有用,但我希望能够在DataGridView中显示用户希望编写的任何查询的结果。
bindingSource.DataSource = new Datasource(result.DataTable);
public static void PostProcessData(ref DataTable dataTable)
{
    // Convert byte[] columns.
    List<DataColumn> colCollRem = new List<DataColumn>();
    List<DataColumn> colCollAdd = new List<DataColumn>();
    foreach(DataColumn col in dataTable.Columns)
        if (col.DataType == typeof(byte[]))
            colCollRem.Add(col);

    // Remove old add new.
    foreach (DataColumn col in colCollRem)
    {
        int tmpOrd = col.Ordinal;
        string colName = String.Format("{0}(Hex)", col.ColumnName);
        DataColumn tmpCol = new DataColumn(colName, typeof(String));
        dataTable.Columns.Add(tmpCol);
        colCollAdd.Add(tmpCol);
        foreach (DataRow row in dataTable.Rows)
            row[tmpCol] = Utilities.ByteArrayToHexString((byte[])row[col]);
        dataTable.Columns.Remove(col);
        string colNameNew = colName.Replace("(Hex)", String.Empty);
        dataTable.Columns[colName].ColumnName = colNameNew;
        dataTable.Columns[colNameNew].SetOrdinal(tmpOrd);
    }
}
public static string ByteArrayToHexString(byte[] p)
{
    byte b;
    char[] c = new char[p.Length * 2 + 2];
    c[0] = '0'; c[1] = 'x';
    for (int y = 0, x = 2; y < p.Length; ++y, ++x)
    {
        b = ((byte)(p[y] >> 4));
        c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
        b = ((byte)(p[y] & 0xF));
        c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
    }
    return new string(c);
}