简体   繁体   English

将一个表列中的所有记录保存在数组中

[英]Save all records from one table column in an array

I want to store all records from a MySQL table column in an array (C#).我想将 MySQL 表列中的所有记录存储在数组(C#)中。

Eg if there is the column "name" in a table:例如,如果表中有列“名称”:

name姓名 other column其他栏目 ... ...
Peter彼得
Marc马克
Henry亨利

... I want to store these names as elements in an array/list programmatically. ...我想以编程方式将这些名称存储为数组/列表中的元素。 The goal is to be able to access each element (in this case name) itself and going on with that.目标是能够访问每个元素(在本例中为名称)本身并继续进行。

Using a MySqlDataReader didn't work out that good, because it only returned me the last record in the column:使用 MySqlDataReader 并没有那么好,因为它只返回了我列中的最后一条记录:

conn.Open();
string getNameQuery = "SELECT * FROM myTable";
MySqlCommand getName = new MySqlCommand(getNameQuery, conn);
dataReader = getName.ExecuteReader();
while(dataReader.Read())
{
    dataReader.getString("name");
}
conn.Close();

Create a List and add the values to it.创建一个List并将值添加到其中。

List<string> ls = new List<string>();
while(dataReader.Read())
{
    ls.Add(dataReader.GetString("name"));
}

Also, as I suggested, if you need only name column then write the query SELECT name FROM myTable .另外,正如我所建议的,如果您只需要name列,请编写查询SELECT name FROM myTable There is no need to fetch other columns.无需获取其他列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有记录从SQL SERVER数据库表保存到数组中? - How to save all records into an array from the SQL SERVER database table? 如何使用WinForms中的C#从一个表中获取所有记录并将其保存到另一表中? - How to get all records from one table and save it on another table by c# in winforms? 如何在C#中基于表格的一列显示记录? - how to display records based on one column from table in c#? 如何从一个表中获取所有记录并基于每个记录获取与 mvc 5 中第一个表记录关联的所有记录 - how to get all records from one table and based on that each record fetch all records which associated to the first table records in mvc 5 将具有标准值的另一个表的列中的所有记录插入到表中 - Insert to a table all records from a column from another table with a standard value 需要从一个表中获取所有记录,这些记录匹配从另一个表返回的值 - Need to get all records from one table, that match values I return from another table 从一个表中选择所有列,从另一表中选择1列 - Select all columns from one table and 1 column from another 根据另一个表中的id列在一个表中插入多个记录 - insert multiple records in one table based on id column in another table 从Azure表存储中获取所有记录 - Get all records from azure table storage 如何将记录从一张表更新到另一张表 - how to update the records from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM