开发者

C# how do I read an mdb(MS Access) file then pass in the retrieved columns selected in an array for later use?

How do I read an mdb file then pass in the retrieved columns selected in an array? Basically, I am trying to retrieve items that match 2 specified criteria columns that then save the last column in an array.

I have the printed results correct as per the two criterias them being qDC and qPL but now when printing them how do I save the last row for later use in an array.

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(""); 
           }

I am trying to use these emails (in column 5) as a mass bcc email chain

Sean


save the columns to variables in your while (reader.Read()) loop. when reader.Read() returns false, you will have the last row values stored in the variables. Then you can do whatever you wish with them.

Update: you can store values from each row in a List<T> object.

Update 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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜