我如何读取 mdb 文件,然后传入在数组中选择的检索列?基本上,我试图检索与 2 个指定条件列匹配的项目,然后将最后一列保存在数组中。
根据这两个标准,我的打印结果是正确的,它们是 qDC 和 qPL,但是现在打印它们时,我如何保存最后一行以供以后在数组中使用。
var list = new List<MyData>();
while (true)
{
while (reader.Read())
{
var data = new MyData
{
ColumnOne = reader.GetString(0),
ColumnTwo = reader.GetString(1),
ColumnThree = reader.GetString(2),
ColumnFour = reader.GetString(3),
ColumnFive = reader.GetString(4),
};
list.Add(data);
Console.WriteLine("");
foreach (var row in list)
{
Console.WriteLine("Start Here");
Console.WriteLine(row.ColumnOne);
Console.WriteLine(row.ColumnTwo);
Console.WriteLine(row.ColumnThree);
Console.WriteLine(row.ColumnFour);
Console.WriteLine(row.ColumnFive);//Email
Console.WriteLine("");
}
我正在尝试将这些电子邮件(在第 5 列中)用作群发密件抄送电子邮件链
肖恩
最佳答案
将列保存到 while (reader.Read())
循环中的变量。当 reader.Read() 返回 false 时,您会将最后一行值存储在变量中。然后你可以用它们做任何你想做的事。
更新: 您可以将每一行的值存储在 List<T> 中对象。
更新 2:
// you need a class to hold all of the columns in a row
class MyData {
public string ColumnOne { get; set; }
public string ColumnTwo { get; set; }
public string ColumnThree { get; set; }
public string ColumnFour { get; set; }
}
// list to hold instances of the class created above
var list = new List<MyData>();
/* code to connect and open reader */
while(reader.Read()) {
// store your row
var data = new MyData {
ColumnOne = reader.GetString(0),
ColumnTwo = reader.GetString(1),
ColumnThree = reader.GetString(2),
ColumnFour = reader.GetString(3),
};
// add it to the list
list.Add(data);
}
// you can loop through the list using a foreach loop
foreach(var row in list) {
Console.WriteLine("Start Here");
Console.WriteLine(row.ColumnOne);
Console.WriteLine(row.ColumnTwo);
Console.WriteLine(row.ColumnThree);
Console.WriteLine(row.ColumnFour);
Console.WriteLine(); // you don't need to pass in an empty string
}
关于C# 如何读取 mdb(MS Access) 文件,然后传入在数组中选择的检索列以供以后使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767608/