我正在使用Visual Studio在C#中创建Windows窗体应用程序。
我正在尝试让它执行以下操作...
For every table in Database:
a. If table name is Cows
i. For every row in this table
1. Create a new item in ‘All_Animals’ dictionary where the Animals ID is the Dictionary Key
and the Key’s value is a new cow object
b. If table name is Dogs
i. same as above …
d. Etc.
我为所有动物创建了子类,其中“动物”为父类。
我现在能做的最好的就是:
private static Dictionary<int, Animal> allAnimals = new Dictionary<int, Animal>();
private void Get_table()
{
try
{
OleDbCommand cmd = null;
//Creates an array of strings containing the table names
String[] Animals = new string[] { "Cows", "Dogs", "Goats", "Sheep" };
//Loop to search through each table for the ID (Cow = 0, Dogs = 1, etc)
for (int i = 0; i < Animals.Length; i++)
{
//Concatenates the element from Animals (table name) to the SQL select-statement to search through that table's records
cmd = new OleDbCommand("SELECT * FROM " + Animals[i].ToString(), conn);
using (OleDbDataReader reader = cmd.ExecuteReader())
{
//if the reader still has rows to read for that table
if (reader.HasRows)
{
//while the reader is reading
while (reader.Read())
{
////for every row create a new object of this class using the column/field attributes
for (int j = 0; j < reader.FieldCount; j++)
{
//Create Dictionary key & create new object from table columns, store object as key's value - e.g. { "1001", "CowObj1001" }
}
}
break;
}
// else no data found for the ID shows feedback for user
else
{
MessageBox.Show("No tables found");
break;
}
}
}
}
catch (Exception ex)
{
error_msg = ex.Message;
MessageBox.Show(error_msg);
}
}
如何从第一列获取ID,使用该ID创建新的哈希表键,然后使用列中的其余值作为该键的值来创建并存储新对象?
我认为每个表都有不同的属性...例如。
母牛= ID,水量,每日成本,体重,年龄,颜色,牛奶量,有刺激性
绵羊= ID,水量,每日成本,重量,年龄,颜色,羊毛量
我的课程...
class Prices
{
public static double milkPriceCow;
public static double milkPriceGoat;
public static double waterPrice;
public static double sheepWoolPrice;
public static double jersyTax;
public static double taxPerKg;
}
abstract class Animal
{
protected int id;
protected double amtWater;
protected double dailyCost;
protected double weight;
protected int age;
protected string color;
public Animal(int id, double amtWater, double dailyCost, double weight, int age, string color)
{
this.id = id;
this.amtWater = amtWater;
this.dailyCost = dailyCost;
this.weight = weight;
this.age = age;
this.color = color;
}
}
class Cow : Animal
{
protected double amtMilk;
protected bool isJersy;
public Cow(int id, double amtWater, double dailyCost, double weight, int age, string color, double amtMilk, bool isJersy) : base(id, amtWater, dailyCost, weight, age, color)
{
this.amtMilk = amtMilk;
this.isJersy = isJersy;
}
}
class Goat : Animal
{
protected double amtMilk;
public Goat(int id, double amtWater, double dailyCost, double weight, int age, string color, double amtMilk) : base(id, amtWater, dailyCost, weight, age, color)
{
this.amtMilk = amtMilk;
}
}
class Sheep : Animal
{
protected double amtWool;
public Sheep(int id, double amtWater, double dailyCost, double weight, int age, string color, double amtWool) : base(id, amtWater, dailyCost, weight, age, color)
{
this.amtWool = amtWool;
}
}
class Dog : Animal
{
public Dog(int id, double amtWater, double dailyCost, double weight, int age, string color) : base(id, amtWater, dailyCost, weight, age, color)
{
}
}
public abstract class Animal
{
public int id { get; private set; }
public double amtWater { get; private set; }
public double dailyCost { get; private set; }
public double weight { get; private set; }
public int age { get; private set; }
public string color { get; private set; }
public Animal(System.Data.IDataRecord record)
{
this.id = Convert.ToInt32(record["id"].ToString());
this.amtWater = Convert.ToDouble(record["amtWater"].ToString());
this.dailyCost = Convert.ToDouble(record["dailyCost"].ToString());
this.weight = Convert.ToDouble(record["weight"].ToString());
this.age = Convert.ToInt32(record["age"].ToString());
this.color = record["color"].ToString();
}
}
public class Cow:Animal
{
public double amtMilk { get; private set; }
public bool isJersy { get; private set; }
public Cow(System.Data.IDataRecord record):base(record)
{
this.amtMilk = Convert.ToDouble(record["amtMilk"].ToString());
this.isJersy = Convert.ToBoolean(record["isJersy"].ToString());
}
}
public class Goat : Animal
{
public double amtMilk { get; private set; }
public Goat(System.Data.IDataRecord record) : base(record)
{
this.amtMilk = Convert.ToDouble(record["amtMilk"].ToString());
}
}
public class Sheep : Animal
{
public double amtWool { get; private set; }
public Sheep(System.Data.IDataRecord record) : base(record)
{
this.amtWool = Convert.ToDouble(record["amtWool"].ToString());
}
}
和扩展方法
public static class DataReaderX
{
public static Animal ToAnimal(this System.Data.IDataRecord record, string typeName)
{
var animal = (Animal)Activator.CreateInstance(Type.GetType($"{typeName}"), record);
return animal;
}
}
然后,当您读取数据时,创建“动物”:
private static Dictionary<int, Animal> allAnimals = new Dictionary<int, Animal>();
public void Get_table(OleDbConnection conn)
{
String[] Animals = new string[] { "Cow", "Dog", "Goat", "Sheep" };
foreach(string animaltype in Animals)
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + animal, conn))
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
{
//MessageBox.Show("No tables found");
continue;
}
while (reader.Read())
{
int someAnimalId = 0;//<---- your animal id;
var animal = reader.ToAnimal(animaltype);
allAnimals.Add(someAnimalId, animal);
}
}
}
}
}