这就是我(在工作中)学习创建类、使用 readRecord 方法创建数据库访问对象以从数据库中获取信息的方式。我很好奇,SF 社区对缩短它的建议是什么?我可以通过将 readRecord 代码向上移动到 DAO 方法中并在其中更改它来摆脱几行代码,但这仍然存在可能有数百行代码的问题......仅用于类,并用于在 DAO 中获取这些属性的值。
我知道在课堂上没有什么可做的(我的意思是,你不能只自动生成一堆属性......Visual Studio 不知道我要做什么!),但是对于 DAO ,有没有办法遍历类中的所有属性,确定它们的类型,然后获取信息?我讨厌输入数百个“property = dr.GetVariableType("variablename");” 一遍又一遍地!
抱歉代码长度...我复制了 6 次属性以了解我在说什么。
班级
namespace MySoftware
{
public class User
{
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string MI { get; set; }
public string suffix { get; set; }
public string email { get; set; }
public string phone{ get; set; }
public string SSN { get; set; }
public int roleId { get; set; }
public bool IsTemp { get; set; }
}
}
道
namespace MySoftware
{
public static List<User> GetUsers(int departmentId)
{
List<User> user = new List<User>();
try
{
using (SqlConnection conn = ConnectionHelper.GetConnection("connectionString"))
{
SqlCommand cmd = new SqlCommand("GetUsers", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DeptId", departmentId);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
users.Add(readUserRecord(dr));
}
}
return users;
}
}
catch (Exception ex)
{
throw new Exception("Failed to get users", ex);
}
}
}
然后我阅读了记录(这通常位于 DAO 的底部。
public static User ReadRegRecord(SqlDataReader dr)
{
User user = new User();
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
user.userId = dr.GetInt32("Id");
user.userName = dr.GetString("UserName");
user.schoolId = dr.GetInt32("schoolId");
user.roleId = dr.GetInt32("roleId");
user.Active = dr.GetBoolean("Active");
user.IsTemp = dr.GetBoolean("IsTemp ");
user.firstName = dr.GetString("firstName");
user.MI = dr.GetString("MI");
user.lastName = dr.GetString("lastName");
user.suffix = dr.GetString("Suffix");
user.email = dr.GetString("email");
user.Phone = dr.GetString("Phone ");
user.SSN = dr.GetString("SSN");
return user;
}
}
例如,有没有办法为 DAO/readRecord 做这样的事情?(我知道这不起作用,但如果有人能想到类似的东西,我很想听听)。
foreach (var prop in student.GetType().GetProperties())
{
if (prop.PropertyType == "String")
{
prop.Value = dr.GetString(prop.Name.ToString());
}
if (prop.PropertyType == "Int")
{
prop.Value = dr.GetInt32(prop.Name.ToString());
}
}