我在SQL Server 2012中有一个包含70列的表。我在ASP.NET中使用3层体系结构。如何使用最少的代码将值存储在该表中。
以下是要存储在表格中的属性。
public int DeptID { get; set; }
public int ProgID { get; set; }
public string Session { get; set; }
public string Name { get; set; }
public string FName { get; set; }
public string FOccupation { get; set; }
public string FIncome { get; set; }
public string FNIC { get; set; }
public string ANIC { get; set; }
public string DBirth { get; set; }
public string Gender { get; set; }
public string Religion { get; set; }
public string Martial_Status { get; set;}
public string Nationality { get; set; }
public string Domicile { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public string PAddress { get; set; }
public string MAddress { get; set; }
public string Service { get; set; }
public string Designation { get; set; }
public string Organization { get; set; }
public int SSCYear { get; set; }
public string SSCEBody { get; set; }
public int SSCRNo { get; set; }
public int SSCTMarks { get; set; }
public int SSCOMarks { get; set; }
public string SSCDivision { get; set; }
public string SSCMSubjects { get; set; }
public int IntermidiateYear { get; set; }
public string IntermidiateEBody { get; set; }
public int IntermidiateRNo { get; set; }
public int IntermidiateTMarks { get; set; }
public int IntermidiateOMarks { get; set; }
public string IntermidiateDivision { get; set; }
public string IntermidiateMSubjects { get; set; }
public int UGraduate1Year { get; set; }
public string UGraduate1EBody { get; set; }
public int UGraduate1RNo { get; set; }
public int UGraduate1TMarks { get; set; }
public int UGraduate1OMarks { get; set; }
public string UGraduate1Division { get; set; }
public string UGraduate1MSubjects { get; set; }
public int UGraduate2Year { get; set; }
public string UGraduate2EBody { get; set; }
public int UGraduate2RNo { get; set; }
public int UGraduate2TMarks { get; set; }
public int UGraduate2OMarks { get; set; }
public string UGraduate2Division { get; set; }
public string UGraduate2MSubjects { get; set; }
public int GraduateYear { get; set; }
public string GraduateEBody { get; set; }
public int GraduateRNo { get; set; }
public int GraduateTMarks { get; set; }
public int GraduateOMarks { get; set; }
public string GraduateDivision { get; set; }
public string GraduateMSubjects { get; set; }
public int GatGeneralRNo { get; set; }
public int GatGeneralScore { get; set; }
public string GatGeneralFStudy { get; set; }
public string GatGeneralTestDate { get; set; }
public string GatGeneralValid { get; set; }
public int GatSubjectRNo { get; set; }
public int GatSubjectScore { get; set; }
public string GatSubjectFStudy { get; set; }
public string GatSubjectTestDate { get; set; }
public string GatSubjectValid { get; set; }
这是调用程序的功能
public DataTable GraduateProgInsert()
{
SqlParameter[] prm = new SqlParameter[72];
//how to add these values in the SqlParameter
}
答案 0 :(得分:1)
您可以使用反射来遍历所有属性并将它们存储在数组中:
foreach(var property in myClass.GetType().GetProperties())
{
SqlParameter newParam = new SqlParameter();
newParam.Name = property.Name;
...
// set other fields of the new parameter here and add it to the array
// the logic to determine the exact type of param can get hairy
}
使用ORM是另一种选择,我当然会考虑避免自己编写这种代码。