Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Postgres:datatable中位_字符串(1)列的ArgumentException_C#_Postgresql_Datatable - Fatal编程技术网

C# Postgres:datatable中位_字符串(1)列的ArgumentException

C# Postgres:datatable中位_字符串(1)列的ArgumentException,c#,postgresql,datatable,C#,Postgresql,Datatable,我正在尝试从简单选择填充数据表: string queryTableData = string.Format("select * from {0}", fullTableName); var command = new NpgsqlCommand(queryTableData, connection); DataTable table = new DataTable(); table.Load(command.ExecuteReader()); 但如果表中有位列: column_na

我正在尝试从简单选择填充数据表:

 string queryTableData = string.Format("select * from {0}", fullTableName);

 var command = new NpgsqlCommand(queryTableData, connection);
 DataTable table = new DataTable();
 table.Load(command.ExecuteReader());
但如果表中有
列:

column_name: column1
data_type: bit
character_maximum_length: 1
udt_name: bit
我得到
参数异常

Type of value has a mismatch with column typeCouldn't store <True> in column1 Column.  Expected type is BitString.
值的类型与列类型不匹配无法存储在列1列中。应为位字符串类型。

有没有办法解决这个问题?我在谷歌搜索解决方案时失败了。

在PostgreSQL方面,
true
是一种数据类型,
bit
是另一种数据类型。它们不兼容

true
为布尔值;不能将布尔值强制转换为位

select cast(true as bit)

如果我正确理解您的错误,那么在数据库端键入Boolean,在DataTable端键入bit(n)。如果不能以某种方式使这些类型兼容(即不同),可以使用CASE表达式在SQL中转换该类型。不过,我不确定这是否适用于您的代码

select case when column1 = true then b'1'
            when column1 = false then b'0'
            -- Think about what to do with NULL.
       end as boolean_to_bit
from your_table_name;
布尔\u到\u位 “比特” -- 1. 0 (空)
我不想比较,我也不打算投任何东西。我所需要的只是将数据读取到空数据表中。除了加入information_schema.columns并转换为不会失败的类型之外,还有其他方法吗?我相信问题在于DataTable本身,令人惊讶的是它无法读取数据并在内部转换。
select cast(b'1' as boolean);
ERROR: cannot cast type bit to boolean
select cast(1 as boolean);
select cast(42 as boolean);
select cast('1' as boolean);  -- But cast('42' as boolean) throws a syntax error
select cast('y' as boolean);
select cast('t' as boolean);
select cast('true' as boolean);
select cast('yes' as boolean);
select case when column1 = true then b'1'
            when column1 = false then b'0'
            -- Think about what to do with NULL.
       end as boolean_to_bit
from your_table_name;
boolean_to_bit "bit" -- 1 0 (null)