If you just want to get a list of all messages from the database, you can use the code below. Note List instead of Array . Easier to use and faster.
public class Message { string Message; string Code; } public class MessageInitializer { DataSet ds; DataRow dr; List<Message> ms; MessageInitializer() { this.ms = new List<Message>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms.Add(new Message { Code = dr.ItemArray[0].ToString(), Message = dr.ItemArray[1].ToString(), }); } } }
You mentioned that you have a couple million records. List will execute just fine if you want to access items in a sequence. If you want to access elements in a non-sequential manner, I suggest using Dictionary instead (to improve your search):
public class MessageInitializer { DataSet ds; DataRow dr; Dictionary<string, Message> ms; MessageInitializer() { this.ms = new Dictionary<string, Message>(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { dr = ds.Tables[0].Rows[i]; ms.Add(dr.ItemArray[0].ToString(), new Message { Code = dr.ItemArray[0].ToString(), Message = dr.ItemArray[1].ToString(), }); } } }
You can access the message as follows:
var message = ms["001-001-001"];
This will be an order or magnitude faster than accessing a random List element:
var message - ms.First(x => x.Code == "001-001-001");
source share