REDAN

REDAN

导航

DataSet.Relations 获取用于将表链接起来并允许从父表浏览到子表的关系的集合。

   private void MakeRelations()

{

MakeRelations(ref objSelectedClasses,objSelectedLesson.Tables[0],13,8);
   return objSelectedClasses;
  }



  private void BuildRelations(ref DataSet objDS,DataTable objChildTab,int parentCol,int childCol)
  {
   objDS.Tables[0].TableName="ClassInfo";
   objDS.Tables.Add(objChildTab.Copy());
   objDS.Tables[1].TableName="LessonInfo";
   objDS.Relations.Add(objDS.Tables["ClassInfo"].Columns[parentCol],objDS.Tables["LessonInfo"].Columns[childCol]);
  }


 

            //ADOSample3\form.cs
            private void button1_Click(object sender, System.EventArgs e)
            
{
                
//create a dataset
                DataSet ds=new DataSet("XMLProducts");
                
//connect to the northwind database and 
                
//select all of the rows from products table and from suppliers table
                
//make sure you connect string matches you server configuration
                
                
string source = Login.Connection;
                
                SqlConnection conn
=new SqlConnection(source);
                
                SqlDataAdapter daProd
=new SqlDataAdapter("SELECT * FROM Products",conn);
                SqlDataAdapter daSup
=new SqlDataAdapter("SELECT * FROM Suppliers",conn);
                
//Fill DataSet from both SqlAdapters
                daProd.Fill(ds,"products");
                daSup.Fill(ds,
"suppliers");
                
//Add the relation
                ds.Relations.Add(ds.Tables["suppliers"].Columns["SupplierId"],
                    ds.Tables[
"products"].Columns["SupplierId"]);
                
//Write the Xml to a file so we can look at it later
                ds.WriteXml("..\\SuppProd.xml",XmlWriteMode.WriteSchema);
                
//load data into grid
                dataGrid1.DataSource=ds;
                dataGrid1.DataMember
="suppliers";
                
//create the XmlDataDocument
                doc=new XmlDataDocument(ds);
                
//Select the productname elements and load them in the grid
                XmlNodeList nodeLst=doc.SelectNodes("//ProductName");
     
                
foreach(XmlNode nd in nodeLst)
                    listBox1.Items.Add(nd.InnerXml);
    
            }

posted on 2008-10-16 16:39  REDAN  阅读(297)  评论(0)    收藏  举报