2010-05-14 46 views
1

我想在查询完成后向数据集添加额外的列,并用数据填充新列。我有以下的数据库关系:C#数据集动态添加DataColumn

EmployeeGroups 
    /  \  
Groups  Employees 

Empoyees持有该个人所有的数据,我将其命名为唯一键的用户名。

团体拥有雇员可以成为其中一员的所有团体,即超级用户,管理员,用户;等等。我将命名唯一密钥GroupID

EmployeeGroups拥有每个员工所属的组的所有关联。 (UserID | GroupID)

我想要完成的是在查询所有用户后,我想循环访问每个用户,并通过向名为'Groups'的数据集添加新列来添加用户所属的组。用一个字符串来插入用户所属的组。然后通过使用数据绑定填充所有员工及其组织关联的列表视图

我的代码如下;位置5是我试图添加到数据集的新列。

string theQuery = "select UserID, FirstName, LastName, EmployeeID, Active from Employees"; 
DataSet theEmployeeSet = itsDatabase.runQuery(theQuery); 
     DataColumn theCol = new DataColumn("Groups", typeof(string)); 
     theEmployeeSet.Tables[0].Columns.Add(theCol); 
     foreach (DataRow theRow in theEmployeeSet.Tables[0].Rows) 
     { 
      theRow.ItemArray[5] = "1234"; 
     } 

目前,该代码将创建新列,但是当我的数据分配给该列没有将被分配,我缺少什么?

如果有任何进一步的分析或信息可以提供,请告诉我。

谢谢大家

编辑
更改过的数据库图,是错误的

+0

你有问题吗? – 2010-05-14 23:59:17

+0

当我正在格式化问题的过程中,我意外地错过了添加问题部分。对于那个很抱歉 – Wesley 2010-05-15 00:02:28

回答

0

当您使用itemarray您必须一次设置所有值。
这样:

theRow.ItemArray = new object[] { x,y,z,s,d }; 

我同意这是不明确的,你应该使用这种方式..
我想getter创建数据的副本,并填写该副本的元素5

你应该这样做:

theRow["Groups"] = "1234" 

或者:

theRow[5] = "1234" 
0

假设有你的员工/团体和协会之间的关系完整性,您可以把表中的一个DataSet,加DataRelation s到定义表之间的关系,然后创建DataColumn S IN的关联表使用DataColumn.Expression来从相关表中查找值。这会给你一个EmployeeGroups表,可以显示每个员工和组的名称。

不幸的是,如果你想以另一种方式,即在Employees表中创建一个将所有组合在一起的列(这就是你所描述的),你不能用表达式来实现。在这种情况下,可能最容易做到你描述的内容,并将一列添加到Employees表中。但LINQ使填充该列非常简单,并且如果您已经创建表之间的关系,并且EmployeeGroups表中的列可以获得员工和组名,那么更好。

这给我们带来了什么。这是一个用Employees,Groups和EmployeeGroups表创建和填充数据集的类。它创建关联表和其他两个表之间的关系,其中DataColumn.ExpressionDataRow.GetChildRows需要为了做他们的魔术。这也暴露了表为WPF可以绑定到IList类型:

using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Linq; 

namespace JoinAndGroupDemo 
{ 
    public class DataSource 
    { 
     private DataSet _DS; 

     public DataSource() 
     { 
      CreateDataSet(); 
     } 

     private void CreateDataSet() 
     { 
      _DS = new DataSet(); 
      DataTable emp = _DS.Tables.Add("Employees"); 
      DataTable grp = _DS.Tables.Add("Groups"); 
      InitTable(emp); 
      InitTable(grp); 

      DataTable assoc = _DS.Tables.Add("EmployeeGroups"); 
      assoc.Columns.Add("empId", typeof (int)); 
      assoc.Columns.Add("grpId", typeof (int)); 

      _DS.Relations.Add(new DataRelation("FK_EmployeeGroups_Employees", 
       emp.Columns["id"], assoc.Columns["empId"])); 
      _DS.Relations.Add(new DataRelation("FK_EmployeeGroups_Groups", 
       grp.Columns["id"], assoc.Columns["grpId"])); 

      assoc.Columns.Add("emp_name"); 
      assoc.Columns["emp_name"].Expression = "Parent(FK_EmployeeGroups_Employees).name"; 
      assoc.Columns.Add("grp_name"); 
      assoc.Columns["grp_name"].Expression = "Parent(FK_EmployeeGroups_Groups).name"; 

      emp.Rows.Add(new object[] { 1, "Malcolm Reynolds"}); 
      emp.Rows.Add(new object[] { 2, "Zoe Washburne" }); 
      emp.Rows.Add(new object[] { 3, "Hoban Washburne" }); 
      emp.Rows.Add(new object[] { 4, "Irina Serra" }); 
      emp.Rows.Add(new object[] { 5, "Jayne Cobb" }); 
      emp.Rows.Add(new object[] { 6, "Kaylee Frye" }); 
      emp.Rows.Add(new object[] { 7, "Simon Tam" }); 
      emp.Rows.Add(new object[] { 8, "River Tam" }); 
      emp.Rows.Add(new object[] { 9, "Derrial Book" }); 

      grp.Rows.Add(new object[] { 1, "Command"}); 
      grp.Rows.Add(new object[] { 2, "Combat" }); 
      grp.Rows.Add(new object[] { 3, "Operations" }); 
      grp.Rows.Add(new object[] { 4, "Other" }); 

      assoc.Rows.Add(new object[] { 1, 1 }); 
      assoc.Rows.Add(new object[] { 2, 1 }); 
      assoc.Rows.Add(new object[] { 1, 2 }); 
      assoc.Rows.Add(new object[] { 2, 2 }); 
      assoc.Rows.Add(new object[] { 5, 2 }); 
      assoc.Rows.Add(new object[] { 8, 2 }); // spoiler alert! 
      assoc.Rows.Add(new object[] { 3, 3 }); 
      assoc.Rows.Add(new object[] { 6, 3 }); 
      assoc.Rows.Add(new object[] { 4, 4 }); 
      assoc.Rows.Add(new object[] { 7, 4 }); 
      assoc.Rows.Add(new object[] { 8, 4 }); 
      assoc.Rows.Add(new object[] { 9, 4 }); 

      emp.Columns.Add("groups", typeof (string)); 
      foreach (DataRow empRow in emp.Rows) 
      { 
       empRow["groups"] = string.Join(
        ", ", 
        empRow 
         .GetChildRows("FK_EmployeeGroups_Employees") 
         .AsEnumerable() 
         .Select(x => (string) x["grp_name"]) 
         .ToArray()); 
      } 

      grp.Columns.Add("employees", typeof(string)); 
      foreach (DataRow grpRow in grp.Rows) 
      { 
       grpRow["employees"] = string.Join(
        ", ", 
        grpRow 
         .GetChildRows("FK_EmployeeGroups_Groups") 
         .AsEnumerable() 
         .Select(x => (string)x["emp_name"]) 
         .ToArray()); 
      } 
     } 

     private void InitTable(DataTable t) 
     { 
      t.Columns.Add("id", typeof (int)); 
      t.Columns.Add("name", typeof (string)); 

      // this is required by DataRelations 
      t.PrimaryKey = new DataColumn[] { t.Columns["id"]}; 
     } 

     public IList Employees 
     { 
      get 
      { return ((IListSource)_DS.Tables["Employees"]).GetList(); } 
     } 

     public IList Groups 
     { 
      get { return ((IListSource)_DS.Tables["Groups"]).GetList(); } 
     } 

     public IList EmployeeGroups 
     { 
      get { return ((IListSource)_DS.Tables["EmployeeGroups"]).GetList(); }    
     } 
    } 
} 

而这里的XAML用于显示所有这些信息的窗口:

<Window x:Class="JoinAndGroupDemo.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:JoinAndGroupDemo" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:DataSource x:Key="DataSource"/> 
     <Style TargetType="Label"> 
      <Style.Setters> 
       <Setter Property="Background" Value="Navy"/> 
       <Setter Property="Foreground" Value="White"/> 
       <Setter Property="HorizontalAlignment" Value="Stretch"/> 
       <Setter Property="HorizontalContentAlignment" Value="Center"/> 
      </Style.Setters> 
     </Style> 
    </Window.Resources> 
    <DockPanel DataContext="{StaticResource DataSource}"> 
     <DockPanel DockPanel.Dock="Left"> 
      <Label DockPanel.Dock="Top">Employees</Label> 
      <ListView ItemsSource="{Binding Employees}"> 
       <ListView.View> 
        <GridView> 
         <GridView.Columns> 
          <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"/> 
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=name}"/> 
          <GridViewColumn Header="Groups" DisplayMemberBinding="{Binding Path=groups}"/> 
         </GridView.Columns> 
        </GridView> 
       </ListView.View> 
      </ListView> 
     </DockPanel> 
     <DockPanel DockPanel.Dock="Left"> 
      <Label DockPanel.Dock="Top">Groups</Label> 
      <ListView ItemsSource="{Binding Groups}"> 
       <ListView.View> 
        <GridView> 
         <GridView.Columns> 
          <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"/> 
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=name}"/> 
          <GridViewColumn Header="Employees" DisplayMemberBinding="{Binding Path=employees}"/> 
         </GridView.Columns> 
        </GridView> 
       </ListView.View> 
      </ListView> 
     </DockPanel> 
     <DockPanel> 
      <Label DockPanel.Dock="Top">Employee groups</Label> 
      <ListView ItemsSource="{Binding EmployeeGroups}"> 
       <ListView.View> 
        <GridView> 
         <GridView.Columns> 
          <GridViewColumn Header="Employee" DisplayMemberBinding="{Binding Path=emp_name}"/> 
          <GridViewColumn Header="Group" DisplayMemberBinding="{Binding Path=grp_name}"/> 
         </GridView.Columns> 
        </GridView> 
       </ListView.View> 
      </ListView> 
     </DockPanel> 
    </DockPanel> 
</Window>