Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建从表中的一列到另一个表中的两列的数据关系_C#_.net - Fatal编程技术网

C# 如何创建从表中的一列到另一个表中的两列的数据关系

C# 如何创建从表中的一列到另一个表中的两列的数据关系,c#,.net,C#,.net,我有一个名为test\u results的数据表,包含以下列: 测试结果\u id 学生证 主题 得分 以及一个名为students的数据表,其中包含以下列: 学生证 我想在表之间创建一个数据关系,这样我就可以得到所有test\u结果行,其中 主题=‘数学’ 学生id=x 其中x是来自students 我想这样做,以便我可以快速循环学生并找到他们的数学结果: List<DataRow> mathResults = new List<DataRow>(); for

我有一个名为
test\u results
的数据表,包含以下列:

  • 测试结果\u id
  • 学生证
  • 主题
  • 得分
  • 以及一个名为
    students
    的数据表,其中包含以下列:

    • 学生证
    我想在表之间创建一个数据关系,这样我就可以得到所有
    test\u结果
    行,其中

    • 主题=‘数学’
    • 学生id=x
    其中x是来自
    students

    我想这样做,以便我可以快速循环学生并找到他们的数学结果:

    List<DataRow> mathResults = new List<DataRow>();
    
    foreach(DataRow resultRow in studentResults){
      if(Convert.ToString(resultRow["subject"]).Trim().ToUpper() == "MATH")
      {
        mathResults.Add(resultRow);
      } 
    }
    
    foreach(数据行[]学生中的学生){
    DataRow[]mathResults=student.GetChildRows(“关系学生数学结果”);
    foreach(mathResults中的DataRow[]mathResult){
    //做点什么
    }
    }
    
    初始化数据关系非常简单;您可以使用基本构造函数。在您的情况下,类似于:

    DataRelation studentResultsRelation = new DataRelation("StudentResults", students.Columns["student_id"], test_results.Columns["student_id"]);
    
    parentDataSet.Relations.Add(studentResultsRelation);
    
    其中parentDataSet是包含两个数据表的数据集

    然而,这就是它变得有点棘手的地方。不能直接查询数据关系,因为它只定义了两个表之间的关系。您可以做的是:

    DataRelation studentResultsRelation = new DataRelation("StudentResults", students.Columns["student_id"], test_results.Columns["student_id"]);
    
    parentDataSet.Relations.Add(studentResultsRelation);
    
    1) 查找与所需学生匹配的行:

    int studentId = 42;
    
    DataRow[] studentRow = students.Select(String.Format("student_id = {0}"), studentId);
    
    2) 然后,您可以利用DataRelation检索所有这些结果:

    //Assumes 1 student row matched your query! Check for 0 rows and more than 1, too!
    DataRow[] studentResults = studentRow[0].GetChildRows(studentResultsRelation);
    
    然后,您可以循环这些以查找数学结果:

    List<DataRow> mathResults = new List<DataRow>();
    
    foreach(DataRow resultRow in studentResults){
      if(Convert.ToString(resultRow["subject"]).Trim().ToUpper() == "MATH")
      {
        mathResults.Add(resultRow);
      } 
    }
    
    List mathResults=new List();
    foreach(studentResults中的DataRow resultRow){
    if(Convert.ToString(resultRow[“subject”]).Trim().ToUpper()=“MATH”)
    {
    mathResults.Add(resultRow);
    } 
    }
    
    我可以看到您已经拥有了大部分,我理解您想要对数据关系做什么;但是,我不认为您可以直接使用它—相反,您首先必须在子表(GetParentRow[s])或父表(GetChildRow[s])中找到所需的行,然后该关系允许您快速找到匹配的行集。然后,您可以根据需要过滤这些内容

    另外,这是一个更简单的数据库查询练习


    希望这有帮助

    初始化数据关系非常简单;您可以使用基本构造函数。在您的情况下,类似于:

    DataRelation studentResultsRelation = new DataRelation("StudentResults", students.Columns["student_id"], test_results.Columns["student_id"]);
    
    parentDataSet.Relations.Add(studentResultsRelation);
    
    其中parentDataSet是包含两个数据表的数据集

    然而,这就是它变得有点棘手的地方。不能直接查询数据关系,因为它只定义了两个表之间的关系。您可以做的是:

    DataRelation studentResultsRelation = new DataRelation("StudentResults", students.Columns["student_id"], test_results.Columns["student_id"]);
    
    parentDataSet.Relations.Add(studentResultsRelation);
    
    1) 查找与所需学生匹配的行:

    int studentId = 42;
    
    DataRow[] studentRow = students.Select(String.Format("student_id = {0}"), studentId);
    
    2) 然后,您可以利用DataRelation检索所有这些结果:

    //Assumes 1 student row matched your query! Check for 0 rows and more than 1, too!
    DataRow[] studentResults = studentRow[0].GetChildRows(studentResultsRelation);
    
    然后,您可以循环这些以查找数学结果:

    List<DataRow> mathResults = new List<DataRow>();
    
    foreach(DataRow resultRow in studentResults){
      if(Convert.ToString(resultRow["subject"]).Trim().ToUpper() == "MATH")
      {
        mathResults.Add(resultRow);
      } 
    }
    
    List mathResults=new List();
    foreach(studentResults中的DataRow resultRow){
    if(Convert.ToString(resultRow[“subject”]).Trim().ToUpper()=“MATH”)
    {
    mathResults.Add(resultRow);
    } 
    }
    
    我可以看到您已经拥有了大部分,我理解您想要对数据关系做什么;但是,我不认为您可以直接使用它—相反,您首先必须在子表(GetParentRow[s])或父表(GetChildRow[s])中找到所需的行,然后该关系允许您快速找到匹配的行集。然后,您可以根据需要过滤这些内容

    另外,这是一个更简单的数据库查询练习


    希望这有帮助

    使用GetChildRows和数据关系比使用Select查询要快得多,当我在student_ide上使用简单连接测试它时,像上面的例子那样开发子关系应该足够快吧?如果您愿意,您可以随时将这些信息存储为一组链表,甚至是由学生id键入的字典。出于兴趣,当您提到select查询时,您的意思是针对数据库还是针对数据表?这些表简化了我的实际代码,速度非常重要。Select(String.Format(“student_id={0}”)、studentId)比使用DataRelation和students.GetChildRows(…)慢很多。获取具有关系的学生的所有测试结果,然后查询这些结果以进行“数学”测试要快得多。在OP中,我想知道这是否可以在一个关系中完成。使用GetChildRows和数据关系比使用Select查询要快得多,当我在student_ide上使用简单的连接进行测试时,像上面的例子那样开发子关系应该足够快吧?如果您愿意,您可以随时将这些信息存储为一组链表,甚至是由学生id键入的字典。出于兴趣,当您提到select查询时,您的意思是针对数据库还是针对数据表?这些表简化了我的实际代码,速度非常重要。Select(String.Format(“student_id={0}”)、studentId)比使用DataRelation和students.GetChildRows(…)慢很多。获取具有关系的学生的所有测试结果,然后查询这些结果以进行“数学”测试要快得多。在OP中,我想知道这是否可以在一个关系中实现。