C# 如何读取 mdb(MS Access) 文件,然后传入在数组中选择的检索列以供以后使用?

标签 c#

我如何读取 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/

相关文章:

c# - 设置文件压缩属性

c# - NancyFX 中的静态内容与 ASP.Net Core

c# - Directshow.net 不检测 Windows 7 中的所有麦克风

c# - 从可为 null 的变量中获取类型

c# - 什么是 Rhino Mocks Repeat?

c# - 当它似乎被添加到项目时,引用丢失的错误

c# - 使用 C++ 或 C# 打开之前的全局检测文件信息

c# - 仅匹配不以零开头的数字的正则表达式

c# - WCF REST 服务客户端配置文件为空

c# - 尝试读取性能计数器时出现 Azure 自动缩放异常