0

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));
}

I get the following exception:

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);
}

it returns ["64","0","0","64","7","95","61","28","165","60","45","200"] which is not what I want.

1
  • Why the sqlite tag? Commented Mar 26, 2018 at 12:43

1 Answer 1

1

Please try:

string[,] result = row["points"] as string[,]; 

pgSql has an array field defined, so it cannot convert to string directly.

Sign up to request clarification or add additional context in comments.

2 Comments

please edit the answer to: string[,] result = row["points"] as string[,]; so that i can mark it as correct.. The array is 2 dimensional so it needs to be string[,]
I am sorry for my mistake and thank you for the correction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.