举例的数据表里有
Student表
public int ID
public string UserName
public string Password
public int Age
public bool IsBoy
public int SchoolID
public decimal Height
public DateTime AddTime
School表
public int ID
public string SchoolName
Student表里面的Height表示身高,用的decimal类型,只是为了演示一下该类型字段而已
SchoolID为学校代码外键,其它意义很明显,就不做介绍了
代码如下 School school = new School(); school.SchoolName = "复旦大学"; school.Add(); school.New(); school.SchoolName = "南京大学"; school.Add(); school.New(); school.SchoolName = "西安交通大学"; school.Add(); Student student = new Student(); student.UserName = "小红"; student.SchoolID = 1; student.Password = "xiaohong"; student.IsBoy = false; student.Height = (decimal)1.623; student.Age = 19; student.AddTime = DateTime.Now; student.Add(); student.New(); student.UserName = "小刚"; student.AddTime = DateTime.Now; student.Age = 17; student.IsBoy = true; student.SchoolID = 2; student.Add(); | |
|
执行以上代码就可以插入数据了 School表3条 ,Student表2条 执行后结果如下 ![](https://p-blog.csdn.net/images/p_blog_csdn_net/ysuhy/EntryImages/20100103/3adf3144abc94b0bcffca3c8.jpg) | |