最近在学习asp.net高级编程,也在尝试在vs05中实现书中的代码,呵呵,今天正好到dataset的表间关系,才发现自己对dataset的了解是多么的少,到现在我也只能说知之甚微,但是把自己今天的过程贴出来,迎接大家的拍砖……
数据库我直接用了wrox的wroxbooks,估计大家都知道,我就不说了。不过我还是给出表的简单结构
BookPrice | BookList | BookAuthors |
添加表间关系的思路是,先创建一个dataset,
DataSet objDataSet = new DataSet();
try
{
objConnection.Open();
objCommand.CommandText = strSelecTitleAuthor;
objDataAdapter.Fill(objDataSet, "BookList");
objCommand.CommandText = strSelctAuthors;
objDataAdapter.Fill(objDataSet, "BookAuthors");
objCommand.CommandText = strSelectPrices;
objDataAdapter.Fill(objDataSet, "BookPrices");
objConnection.Close();
}
catch (Exception objError)
{
// display error details
Page.Response.Write( objError.Message + objError.Source);
}
try
{
objConnection.Open();
objCommand.CommandText = strSelecTitleAuthor;
objDataAdapter.Fill(objDataSet, "BookList");
objCommand.CommandText = strSelctAuthors;
objDataAdapter.Fill(objDataSet, "BookAuthors");
objCommand.CommandText = strSelectPrices;
objDataAdapter.Fill(objDataSet, "BookPrices");
objConnection.Close();
}
catch (Exception objError)
{
// display error details
Page.Response.Write( objError.Message + objError.Source);
}
然后再添加关系
DataColumn parentCol;
DataColumn childColAuthor;
DataColumn childColPrice;
// Code to get the DataSet not shown here.
parentCol = objDataSet.Tables["BookList"].Columns["ISBN"];
childColAuthor = objDataSet.Tables["BookAuthors"].Columns["ISBN"];
childColPrice = objDataSet.Tables["BookPrices"].Columns["ISBN"];
DataRelation objRelation ;
objRelation = new DataRelation("BookAuthors", parentCol, childColAuthor);
objDataSet.Relations.Add(objRelation);
objRelation = new DataRelation("BookPrices", parentCol, childColPrice);
objDataSet.Relations.Add(objRelation);
DataColumn childColAuthor;
DataColumn childColPrice;
// Code to get the DataSet not shown here.
parentCol = objDataSet.Tables["BookList"].Columns["ISBN"];
childColAuthor = objDataSet.Tables["BookAuthors"].Columns["ISBN"];
childColPrice = objDataSet.Tables["BookPrices"].Columns["ISBN"];
DataRelation objRelation ;
objRelation = new DataRelation("BookAuthors", parentCol, childColAuthor);
objDataSet.Relations.Add(objRelation);
objRelation = new DataRelation("BookPrices", parentCol, childColPrice);
objDataSet.Relations.Add(objRelation);
最后绑定到页面(我这里使用了gridview,才正确现实,使用datalist不能将数据绑定过去。)
GridView1.DataSource = objDataSet.Tables;
GridView1.DataBind();
GridView2.DataSource = objDataSet.Relations;
GridView2.DataBind();
GridView3.DataSource = objDataSet.Tables["BookList"].DefaultView;
GridView3.DataBind();
GridView4.DataSource = objDataSet.Tables["BookAuthors"].DefaultView;
GridView4.DataBind();
GridView5.DataSource = objDataSet.Tables["BookPrices"].DefaultView;
GridView5.DataBind();
GridView1.DataBind();
GridView2.DataSource = objDataSet.Relations;
GridView2.DataBind();
GridView3.DataSource = objDataSet.Tables["BookList"].DefaultView;
GridView3.DataBind();
GridView4.DataSource = objDataSet.Tables["BookAuthors"].DefaultView;
GridView4.DataBind();
GridView5.DataSource = objDataSet.Tables["BookPrices"].DefaultView;
GridView5.DataBind();
完成后的结果……
有两点点注意:
1 那就是用sql的pubs不能完成类似的结果,而且sql语句只能是“Select * From [Authors] where au_id '779-83-776'”
不知道是表的数据的原因,还是其他的原因
2 添加关系时,不能像下面这样了
objRelation = new DataRelation("BookAuthors",
objDataSet.Tables["Books"].Columns["ISBN"],
objDataSet.Tables["Authors"].Columns["ISBN"]);
[注:我是新手,我只能说这些了,更进一步的东西,还是希望大家指点]