How to read array field from database using c#?

情到浓时终转凉″ 提交于 2020-01-03 00:57:44

问题


I am using C# with postgresql. In the database I have a table named test and in this table I have a column named arr that it's datatype is double[] and I stored multiple record like this {1, 1, 2, 3, 0, 5, 1, 4}. Now, how to return those records into C# program and stored in a list for example List<double[]> arr1 = new List<double[]>();

Who can help me?


回答1:


For array datatypes, all you have to do is cast the result as an array of double:

NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();

NpgsqlCommand cmd = new NpgsqlCommand("select arr from test", conn);
NpgsqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    double[] myArray = (double[])reader.GetValue(0);
    // do your bidding
}

reader.Close();



回答2:


Here is a walkthrough, Using PostgreSQL in your C# (.NET): Using PostgreSQL in your C# (.NET)

You will need to give us some specific problems that you are having so we can help solve the problem.



来源:https://stackoverflow.com/questions/39329681/how-to-read-array-field-from-database-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!