[英]How Npgsql deal with cast types
I've noticed the following behavior: 我注意到以下行为:
In pgAdmin I perform this query and I get this result: 在pgAdmin中,我执行以下查询,并得到以下结果:
SELECT
CAST(COALESCE(c.latitude, '-1') as varchar),
CAST(COALESCE(c.longitude, '-1') as varchar)
FROM mytable c WHERE c.acolumn = 'S' AND c.anothercolumn=1
---------------------------------
coalesce coalesce
character varying character varying
"-25.30089" | "-57.625866"
I use Npgsql version 2.2.4.3 in a C# project, I perform the same query using IDbCommand and IDataReader and I get these results: 我在C#项目中使用Npgsql版本2.2.4.3,使用IDbCommand和IDataReader执行相同的查询,并得到以下结果:
IDbCommand cmd = con.CreateCommand();
cmd.Connection = con;
IDataReader reader = null;
cmd.CommandText = "SELECT CAST(COALESCE(c.latitude, '-1') as varchar), CAST(COALESCE(c.longitude, '-1') as varchar) FROM mytable c WHERE c.acolumn = 'S' AND c.anothercolumn=1";
cmd.CommandType = CommandType.Text;
reader = cmd.ExecuteReader();
while (reader.Read())
{
string latitude_r= reader.GetValue(0).ToString();
string longitude_r= reader.GetValue(1).ToString();
}
---------------------------------------------
latitude_r "-25.300889999999999" string
longitude_r "-57.625866000000002" string
Can someone explain to me why? 有人可以向我解释原因吗? is because I'm using the .NET IDbCommand interface? 是因为我使用的是.NET IDbCommand接口? is there a way to get the exact value without those extra decimal values? 没有这些多余的十进制值,有没有办法获得准确的值? this is the postgresql definition of the columns: 这是列的postgresql定义:
ALTER TABLE mytable ADD COLUMN longitude double precision;
ALTER TABLE mytable ADD COLUMN latitude double precision;
Npgsql 2 is very old by now, you should use the latest stable version (3.0.5). Npgsql 2现在已经很老了,您应该使用最新的稳定版本(3.0.5)。
The explanation for what you're seeing is simple - Npgsql 2 set the extra_float_digits
parameter to 3 (see http://www.postgresql.org/docs/current/static/runtime-config-client.html ) - this causes many more digits to be returned, and was necessary because of the way Npgsql transferred data to/from the server. 您所看到的内容的解释很简单extra_float_digits
2将extra_float_digits
参数设置为3(请参阅http://www.postgresql.org/docs/current/static/runtime-config-client.html)-这会导致更多要返回的数字,并且由于Npgsql与服务器之间的数据传输方式所必需。
Npgsql 3 no longer sets this parameter (although you can set it yourself), so you should see the same behavior as in pgAdmin. Npgsql 3不再设置此参数(尽管您可以自己设置),因此您应该看到与pgAdmin中相同的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.