C#从PostgreSQL检索_text类型 [英] c# retrieve _text type from postgresql
本文介绍了C#从PostgreSQL检索_text类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,例如[[ 64, 0, 0],[ 64, 7, 95],[ 61, 28, 165] ,[ 60, 45, 200]]中的[我从Rails中的ruby中存储,我想在我的C#项目中检索它。当我这样做时:
I have a array like [["64", "0", "0"], ["64", "7", "95"], ["61", "28", "165"], ["60", "45", "200"]] in Postgres column which I stored from ruby on rails, and I want to retrieve it in my C# project. When I do this:
NpgsqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetName(0), reader.GetString(1));
}
我遇到以下异常:
System.InvalidCastException: Can't cast database type _text to String
如何获取此列值作为数组
但是当我这样做时:
How can I get this column value as an Array But when I do:
dt.Load(reader);
foreach (DataRow row in dt.Rows)
{
var points = row["points"];
var json = new JavaScriptSerializer().Serialize(points);
Console.WriteLine(json);
}
它返回[ 64, 0, 0, 64, 7, 95, 61, 28, 165, 60, 45, 200]这不是我想要的
it returns ["64","0","0","64","7","95","61","28","165","60","45","200"] which is not what I want
推荐答案
请尝试:
string[,] result = row["points"] as string[,];
pgSql已定义了数组字段,因此无法直接转换为字符串。
pgSql has an array field defined, so it cannot convert to string directly.
这篇关于C#从PostgreSQL检索_text类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文