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



我如何读取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中。对象。

更新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
}

最新更新