C# 是否可以使用byte[]数组字段在Datatable上设置Select()?
我对SQL Server运行查询,以获取包含C# 是否可以使用byte[]数组字段在Datatable上设置Select()?,c#,sql-server,C#,Sql Server,我对SQL Server运行查询,以获取包含varbinary列的表;当作为数据表返回时,列将转换为字节数组 我想知道是否有办法使Select()覆盖System.Byte[] 这就是我所尝试的: byte[] b = (byte[])sessions.table.Rows[72]["security_id"]; string hexString = ByteArrayToHexString(b); DataRow[] rows = users.table.Select("sid = " + h
varbinary
列的表;当作为数据表返回时,列将转换为字节数组
我想知道是否有办法使Select()
覆盖System.Byte[]
这就是我所尝试的:
byte[] b = (byte[])sessions.table.Rows[72]["security_id"];
string hexString = ByteArrayToHexString(b);
DataRow[] rows = users.table.Select("sid = " + hexString);
// throw exception...
// System.Data.SyntaxErrorException: 'Cannot interpret token '0' at position 7.'
不知道,但是DataView过滤器语法非常模糊,很少使用。相反,只需迭代DataTable.Rows并运行比较,或者使用并运行类似以下内容:
var rows = users.table.AsEnumerble().Where(r => r["sid"].SequenceEqual(b)).ToArray();
根据,由于David Browne,datatable无法使用select函数处理二进制数据,因此未实现:
internal enum ValueType {
Unknown = -1,
Null = 0,
Bool = 1,
Numeric = 2,
Str = 3,
Float = 4,
Decimal = 5,
Object = 6,
Date = 7,
}
唯一的解决方案是,如果知道列是什么,在服务器查询中将varbinay转换为varchar,或者在重复表上将datatable byte[]列转换为string列,而不是使用select(),当然,像linq这样的其他组件使其更容易
更新:
示例代码如何向表中添加新的字符串列并将字节[]
转换为字符串
:
private DataTable fixDatatableType(DataTable input) {
List<DataColumn> overwriteColumnIndex = new List<DataColumn>();
List<string> addedColumns = new List<string>();
for (int i = 0; i < input.Columns.Count; i++)
{
if (input.Columns[i].DataType == typeof(byte[])) {
overwriteColumnIndex.Add(input.Columns[i]);
string newColumnName = input.Columns[i].ColumnName + "_str";
addedColumns.Add(newColumnName);
DataColumn dataColumn = new DataColumn(newColumnName, typeof(string));
input.Columns.Add(dataColumn);
}
}
if (overwriteColumnIndex.Count != 0) {
for (int z = 0; z < input.Rows.Count; z++)
{
DataRow row = input.Rows[z];
for (int m = 0; m < overwriteColumnIndex.Count; m++)
{
if (row[overwriteColumnIndex[m]] != DBNull.Value) {
row[addedColumns[m]] =
ByteArrayToHexString((byte[])row[overwriteColumnIndex[m]]);
}
}
}
}
return input;
}
专用数据表fixDatatableType(数据表输入){
List overwriteColumnIndex=新列表();
List addedColumns=新列表();
对于(int i=0;i
例外情况是什么?如果我们没有所有的信息,我们该如何帮助呢?我添加了一个例外,我知道,但问题是针对select()的。源代码表明没有实现对二进制字符串的支持。