将数据库中的字段存储在数组C#中

编程入门 行业动态 更新时间:2024-10-25 16:18:50
本文介绍了将数据库中的字段存储在数组C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将Microsoft Access数据库中某个列中的所有字段存储在一个数组中.我正在使用OleDb,但我真的不知道该怎么做.

I am trying to store all fields from a column in my microsoft access database in an array. I am using OleDb but I don't really know how to go about doing this.

我知道我必须要循环遍历表,因为表中有行,但是我不知道如何将当前字段存储在数组的当前索引中. 任何帮助将不胜感激!

I know that I have to have a loop to go over the table the amount of times as there are rows in the table, but I don't know how to store the current field in the current index of the array. Any help would be greatly appreciated!

以下是其中一些代码段:

Here is a snippet of some of the code:

string[] tasks; string sql = "SELECT [Task Name] FROM Tasks"; OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbDataReader dataReader = cmd.ExecuteReader(); if (dataReader.HasRows) { for (int i = 1; i <= 10; i++) { //tasks[i] = current field in table } }

推荐答案

听起来像您想要这样的东西吗?

Sounds like you want something like this?

string[] tasks; string sql = "SELECT [Task Name] FROM Tasks"; using (OleDbCommand cmd = new OleDbCommand(sql, conn)) { using (OleDbDataReader dataReader = cmd.ExecuteReader()) { List<object[]> list = new List<object[]>(); if (dataReader.HasRows) { while (dataReader.Read()) { object[] oarray = new object[dataReader.FieldCount]; list.Add(oarray); for (int i = 1; i <= dataReader.FieldCount; i++) { oarray[i] = dataReader[i]; } } } } }

更多推荐

将数据库中的字段存储在数组C#中

本文发布于:2023-10-28 19:09:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字段   数据库中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!