Wednesday, 18 September 2013

What is a more general way of creating an arraylist in C# like the one in the example?

What is a more general way of creating an arraylist in C# like the one in
the example?

Objective: Achieve a more general way of creating an arraylist.
Issue: I have to create multiple arraylists, each matched to a unique
structure for the purpose of comparing and updating a table in Sql
database. In the spirit of DRY I am trying to find a better way of
creating each array. The code I am using is as follows
Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
public static void Users()
{
String sql = "";
try
{
conn.Open();
sql = "SELECT" +
"database.dbo.table1.username," +
"database.dbo.table1.status" +
"FROM" +
"database.dbo.table1";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//structure below
User structure_A = new User();
structure_A.username = dr.GetValue(0).ToString();
structure_A.status = dr.GetValue(1).ToString();
//added to arraylist
arraylist_A.Add(structure_A);
}
dr.Close();
conn.Close();
}
Note: More information can be provided as requested. Thank you in advance
for any insight

No comments:

Post a Comment