下面的代码已经是修改过的,运行输出: 1001,张三
static void Main(string[] args)
{
try
{
DataSet ds1=new DataSet("myDataSet");
DataTable dt_student=new DataTable("dt_student");
DataTable dt_course=new DataTable("dt_course");
DataTable dt_score=new DataTable("dt_score");
DataColumn dc_xh=new DataColumn("xh",typeof(string));
DataColumn dc_name=new DataColumn("name",typeof(string));
DataColumn dc_courseid=new DataColumn("courseid",typeof(string));
DataColumn dc_coursename=new DataColumn("coursename",typeof(string));
DataColumn dc_id=new DataColumn("scoreid",typeof(string));
DataColumn dc_scorexh=new DataColumn("scorexh",typeof(string));
DataColumn dc_score_course_id=new DataColumn("score_course_id",typeof(string));
DataColumn dc_score=new DataColumn("score",typeof(string));
dt_student.Columns.Add(dc_xh);
dt_student.Columns.Add(dc_name);
dt_course.Columns.Add(dc_courseid);
dt_course.Columns.Add(dc_coursename);
dt_score.Columns.Add(dc_scorexh);
dt_score.Columns.Add(dc_id);
dt_score.Columns.Add(dc_score_course_id);
dt_score.Columns.Add(dc_score);
// 这三句是从上面移下来的,并且把字符串改成了DataTable的名字了.
ds1.Tables.Add(dt_student);
ds1.Tables.Add(dt_course);
ds1.Tables.Add(dt_score);
DataRelation dr1=new DataRelation("dt_studentdt_score",dc_xh,dc_scorexh);
DataRelation dr2=new DataRelation("dr_course_score",dc_courseid,dc_score_course_id);
ds1.Relations.Add(dr1);
ds1.Relations.Add(dr2);
DataRow myrow=dt_student.NewRow();
myrow["xh"]="1001";
myrow["name"]="张三";
dt_student.Rows.Add(myrow);
foreach(DataRow dr in ds1.Tables[0].Rows)
{
Console.WriteLine("{0},{1}",dr["xh"].ToString(),dr["name"].ToString());
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
System.Threading.Thread.Sleep(2000);
}
}