[C#] 
private void CreateRelation() {
    // Get the DataColumn objects from two DataTable objects in a DataSet.
    DataColumn parentCol;
    DataColumn childCol;
    // Code to get the DataSet not shown here.
    parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
    childCol = DataSet1.Tables["Orders"].Columns["CustID"];
    // Create DataRelation.
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
    // Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder);
 }

解决方案 »

  1.   

    To: upto:
    Access本身没有定义关系,用代码可以修改Access本身吗?To: wangsaohui:gridZong.DataSource = ds;
    gridZong.DataMember = "Zongzhang";//这两句应该怎麽写?
      

  2.   

    gridZong.DataSource = ds.Tables("Zongzhang");
    gridZhi.DataSource = 你新建的关系 ;(类似于例子中的relCustOrder)
      

  3.   

    用代码可以修改Access本身吗?可以的。
      

  4.   

    To: wnagsaohui:
    我在我的代码中加了DataRelation的代码,错误提示:
    未处理的“System.NullReferenceException”类型的异常出现在 ManageSystem.exe 中。其他信息: 未将对象引用设置到对象的实例。strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
    FormCreateDatabase.Path;//返回文件路径
    string strSqlZong = "SELECT * FROM Zongzhang";
    string strSqlZhi = "SELECT * FROM Zhichu";

    OleDbDataAdapter daZong = new OleDbDataAdapter( strSqlZong, strConn );
    OleDbDataAdapter daZhi = new OleDbDataAdapter( strSqlZhi, strConn );
    daZong.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    daZhi.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    DataSet ds = new DataSet( );
    ds.Clear( );DataColumn colZong = ds.Tables[ "Zongzhang" ].Columns[ "DateTime" ];//错误提示
                                                                        //停在此行
    DataColumn colZhi = ds.Tables[ "Zhichu" ].Columns[ "DateTime" ];    DataRelation rel = new DataRelation( "ZongZhi", colZong, colZhi );
    ds.Relations.Add ( rel );

    daZong.Fill( ds );                   /*从此行不知对不对
    daZhi.Fill( ds );gridZong.DataSource = ds;
    gridZong.DataMember = "Zongzhang";gridZhi.DataSource = rel;
    gridZhi.DataMember = "ZongZhi";                          */请帮忙给看一看。多谢。
      

  5.   

    daZong.Fill( ds );                   /*从此行不知对不对
    daZhi.Fill( ds );
    放在
    DataColumn colZong = ds.Tables[ "Zongzhang" ].Columns[ "DateTime" ];//错误提示
    之前
      

  6.   

    DateTime 是SQL Server里面的关键子,可能跟这个有关~最好修改表中相应列的字段名称.或用 [DateTime] ,加方括号
      

  7.   

    daZong.Fill( ds );                   /*从此行不知对不对
    daZhi.Fill( ds );
    改为:daZong.Fill( ds, "Zongzhang" );
    daZhi.Fill( ds, "Zhichu" );
      

  8.   

    把daZong.Fill( ds, "Zongzhang" );
    daZhi.Fill( ds, "Zhichu" );
    放到前面,放到
    DataColumn colZong = ds.Tables[ "Zongzhang" ].Columns[ "DateTime1" ];
    前面
    datetime最好改名字,这是sqlserver的保留字。否则没有填充,DS中还没有datatable和相应的列,如何建立关系呢
      

  9.   

    我已把代码改了一下,但是关系好象还是不存在,请大家再给看看。多谢。strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
    FormCreateDatabase.Path;
    string strSqlZong = "SELECT * FROM Zongzhang";
    string strSqlZhi = "SELECT * FROM Zhichu";

    OleDbDataAdapter daZong = new OleDbDataAdapter( strSqlZong, strConn );
    OleDbDataAdapter daZhi = new OleDbDataAdapter( strSqlZhi, strConn );
    daZong.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    daZhi.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    DataSet ds = new DataSet( );
    ds.Clear( );

    daZong.Fill( ds, "Zongzhang" );
    daZhi.Fill( ds, "Zhichu" );DataRelation rel = new DataRelation( "ZongZhi",
    ds.Tables[ "Zongzhang" ].Columns[ "DateTimeZong" ],
    ds.Tables[ "Zhichu" ].Columns[ "DateTimeZhi" ] );
    ds.Relations.Add( rel );gridZong.DataSource = ds;                        /*是不是DataSource
    gridZong.DataMember = "Zongzhang";                  和DataMember的
                                                         设置问题*/
    gridZhi.DataSource = ds;
    gridZhi.DataMember = "Zhichu";