In a nutshell: I have some database table data that I want to populate c# structs with.
For example:
struct Phone
{
public int PhoneID;
public string Number;
}
struct Patient
{
public Phone WorkPhone;
}
And the SQL tables look like this:
Patient table:
Patient ID, Patient Name, etc.
Phone table:
PhoneID, Number, Phone Type etc.
Basically the structs are mirrors of the SQL tables.
I have the primary key (PhoneID), that I am passing through a select statement. So I will only retrieve one row in the case of the patient or up to 5 rows for the Phone table (They can have Work, Cell, Home, etc).
I don’t have a great way to stuff this table into a struct easily. What I am doing now is one by one. You can see for hundreds of variables per patient, it’s a very tedious and highly code intensive process. Is there some way, maybe I can have a variable in the variable name, for example if the column name is Phone then it would stuff the Phone variable. Hope that makes sense.
query = "SELECT * FROM Phone p WHERE p.StatusID = 'A' AND ObjectTypeID = 'PA' AND ObjectID = '" + PatientID.ToString() + "'";
SqlDataAdapter a1 = new SqlDataAdapter(query, dataConnection);
DataTable t1 = new DataTable();
a1.Fill(t1);
DataTableReader r1 = t.CreateDataReader();
//object value = null;
while (reader.Read())
{
for (int i = 0; i < r1.FieldCount; i++)
{
if (!r1.IsDBNull(i))
{
if reader.GetValue(i)
switch (r1.GetName(i))
{
case "PhoneID": { pr.HomePhone.PhoneID = (int)reader.GetValue(i); break; }
case "PhoneTypeID": { pr.HomePhone.PhoneTypeID = (string)reader.GetValue(i); break; }
case "ObjectID": { pr.HomePhone.ObjectID = (int)reader.GetValue(i); break; }
case "ObjectTypeID": { pr.HomePhone.ObjectTypeID = (string)reader.GetValue(i); break; }
It may be that you need an ORM like NHibernate, Lightspeed or Entity Framework.
If they’d be overkill, check out one of the new mini ORM-like libraries like Massive.