0
我正在創建一個MVC3示例項目而不是實體框架。我創建了一個API(基本上是一個DLL),其中包含教師學校和資格認證課程以及DBOperation課程來管理CRUD操作。MVC3多對多沒有實體框架
教師和資格有多對多的關係(一個特定的教師可以有多個教育資格)。
我設法完成了一對多(教師資格)的代碼併成功創建了控制器和視圖。
但是我遇到了很多很多問題。
需要幫助。謝謝。
public class School
{
public int SchoolID { get; set; }
public string Name { get; set; }
public string Telephone { get; set; }
public string Address { get; set; }
}
public class Teacher
{
public int TeacherID { get; set; }
public string Name { get; set; }
public string NIC { get; set; }
public string Address { get; set; }
public string Telephone { get; set; }
public int School { get; set; }
//public List<Qualification> Qualification { get; set; } if teacher got many Qualifications
public int Qualification { get; set; }
}
public class Qualification
{
public int QualificationID { get; set; }
public string Type { get; set; }
}
public class DBOperations
{
public List<Teacher> GetAllTeachers()
{
List<Teacher> t = new List<Teacher>();
string connectionString = Connection.ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM Teachers";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Teacher teacher = new Teacher();
teacher.TeacherID = dr.GetInt32(0);
teacher.Name = dr.GetString(1);
teacher.NIC = dr.GetString(3);
teacher.Address = dr.GetString(4);
teacher.Telephone = dr.GetString(4);
teacher.School = dr.GetInt32(5);
teacher.Qualification = dr.GetInt32(6);
t.Add(teacher);
}
return t;
}
public void AddNewTeacher(Teacher teacher)
{
string Name = teacher.Name;
string NIC = teacher.NIC;
string Address = teacher.Address;
string Telephone = teacher.Telephone;
int School = teacher.School;
int Qualification = teacher.Qualification;
string connectionString = Connection.ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Teachers VALUES('" + Name + "','" + NIC + "','" + Address + "','" + Telephone + "','" + School + "','" + Qualification + "')";
cmd.ExecuteNonQuery();
conn.Close();
}
}