如何使用VB.NET对具有多个表的数据集执行连接查询

时间:2013-09-13 17:38:55

标签: vb.net dataset

我正在将两个XML文件读入一个数据集。我这样做是通过将我的第一个xml文件读入数据集,将我的第二个xml文件读入另一个数据集然后将两个数据集合并为一个数据集。我不确定这是否是最好和最有效的方法。

xmlFilePath = "C:\RESUSCITATION_INFUSION_DRUGS.xml"
dsInfusions_min.ReadXml(xmlFilePath)

xmlFilePath = "C:\RESUSCITATION_WEIGHT_CAT.xml"
dsWeight.ReadXml(xmlFilePath)

dsMain.Merge(dsInfusions_min.Tables(0))
dsMain.Merge(dsWeight.Tables(0))

所以现在我的dsMain数据集有两个表。我的问题是,如何执行一个select语句来连接两个表,例如下面的select,并将结果合并到另一个数据集中?

SELECT
    D.DRUG_NAME,
    D.DRUG_UNITS,
    D.PATIENT_WEIGHT_CATEGORY
FROM
    RESUSCITATION_INFUSION_DRUGS D, RESUSCITATION_WEIGHT_CAT C
WHERE
    D.PATIENT_WEIGHT_CATEGORY = C.PATIENT_WEIGHT_CATEGORY AND
    15 <  C.MAXIMUM_WEIGHT and
    15 >= C.MINIMUM_WEIGHT
ORDER BY
    D.DRUG_NAME

这是我的两个xml文件......

<?xml version='1.0'  encoding='UTF-8' ?>
<RESUSCITATION_WEIGHT_CATEGORIES>
    <RESUSCITATION_WEIGHT_CATEGORY>
        <PATIENT_WEIGHT_CATEGORY>00</PATIENT_WEIGHT_CATEGORY>
        <MINIMUM_WEIGHT>0</MINIMUM_WEIGHT>
        <MAXIMUM_WEIGHT>9999</MAXIMUM_WEIGHT>
        <CATEGORY_DESCRIPTION>All patients, regardless of weight.   </CATEGORY_DESCRIPTION>
    <UPDATED_WHEN>2003-08-20 12:00:57</UPDATED_WHEN>
</RESUSCITATION_WEIGHT_CATEGORY>
<RESUSCITATION_WEIGHT_CATEGORY>
    <PATIENT_WEIGHT_CATEGORY>01</PATIENT_WEIGHT_CATEGORY>
    <MINIMUM_WEIGHT>0</MINIMUM_WEIGHT>
    <MAXIMUM_WEIGHT>4</MAXIMUM_WEIGHT>
    <CATEGORY_DESCRIPTION>Patients less than 4 Kg.</CATEGORY_DESCRIPTION>
    <UPDATED_WHEN>2003-08-20 12:01:23</UPDATED_WHEN>
</RESUSCITATION_WEIGHT_CATEGORY>
<RESUSCITATION_WEIGHT_CATEGORY>
    <PATIENT_WEIGHT_CATEGORY>02</PATIENT_WEIGHT_CATEGORY>
    <MINIMUM_WEIGHT>4</MINIMUM_WEIGHT>
    <MAXIMUM_WEIGHT>9999</MAXIMUM_WEIGHT>
    <CATEGORY_DESCRIPTION>Patients greater than 4 Kg.</CATEGORY_DESCRIPTION>
    <UPDATED_WHEN>2003-08-20 12:01:52</UPDATED_WHEN>
</RESUSCITATION_WEIGHT_CATEGORY>
<RESUSCITATION_WEIGHT_CATEGORY>
    <PATIENT_WEIGHT_CATEGORY>03</PATIENT_WEIGHT_CATEGORY>
    <MINIMUM_WEIGHT>0</MINIMUM_WEIGHT>
    <MAXIMUM_WEIGHT>5</MAXIMUM_WEIGHT>
    <CATEGORY_DESCRIPTION>Patients less than 5 Kg.</CATEGORY_DESCRIPTION>
    <UPDATED_WHEN>2007-10-18 13:46:43</UPDATED_WHEN>
</RESUSCITATION_WEIGHT_CATEGORY>
<RESUSCITATION_WEIGHT_CATEGORY>
    <PATIENT_WEIGHT_CATEGORY>04</PATIENT_WEIGHT_CATEGORY>
    <MINIMUM_WEIGHT>5</MINIMUM_WEIGHT>
    <MAXIMUM_WEIGHT>9999</MAXIMUM_WEIGHT>
    <CATEGORY_DESCRIPTION>Patients greater than 5 Kg.</CATEGORY_DESCRIPTION>
    <UPDATED_WHEN>2007-10-18 13:47:23</UPDATED_WHEN>
</RESUSCITATION_WEIGHT_CATEGORY>

<?xml version='1.0'  encoding='UTF-8' ?>
<RESUSCITATION_INFUSION_DRUGS>
<RESUSCITATION_INFUSION_DRUG>
    <DRUG_NAME>DOBUTamine</DRUG_NAME>
    <PATIENT_WEIGHT_CATEGORY>07</PATIENT_WEIGHT_CATEGORY>
    <DRUG_CONCENTRATION>12.5</DRUG_CONCENTRATION>
    <DRUG_CONCENTRATION_UNITS>mg/mL</DRUG_CONCENTRATION_UNITS>
    <DOSAGE_RATE>1</DOSAGE_RATE>
    <DOSAGE_RATE_UNITS>mL/h</DOSAGE_RATE_UNITS>
    <DOSAGE_RATIO>2</DOSAGE_RATIO>
    <DOSAGE_RATIO_UNITS>mcg/kg/min</DOSAGE_RATIO_UNITS>
    <SOLUTION_PREPARATION></SOLUTION_PREPARATION>
    <SOLUTION_UNITS>mg in 50 mL D5W</SOLUTION_UNITS>
    <VOLUME_UNITS>mL</VOLUME_UNITS>
    <SOLUTION_CONCENTRATION></SOLUTION_CONCENTRATION>
    <DRUG_VOLUME></DRUG_VOLUME>
</RESUSCITATION_INFUSION_DRUG>
<RESUSCITATION_INFUSION_DRUG>
    <DRUG_NAME>DOPamine</DRUG_NAME>
    <PATIENT_WEIGHT_CATEGORY>03</PATIENT_WEIGHT_CATEGORY>
    <DRUG_CONCENTRATION>3.2</DRUG_CONCENTRATION>
    <DRUG_CONCENTRATION_UNITS>mg/mL</DRUG_CONCENTRATION_UNITS>
    <DOSAGE_RATE></DOSAGE_RATE>
    <DOSAGE_RATE_UNITS>mL/h</DOSAGE_RATE_UNITS>
    <DOSAGE_RATIO>5</DOSAGE_RATIO>
    <DOSAGE_RATIO_UNITS>mcg/kg/min</DOSAGE_RATIO_UNITS>
    <SOLUTION_PREPARATION>160</SOLUTION_PREPARATION>
    <SOLUTION_UNITS>mg in 50 mL D5W</SOLUTION_UNITS>
    <VOLUME_UNITS>mL</VOLUME_UNITS>
    <SOLUTION_CONCENTRATION>3200</SOLUTION_CONCENTRATION>
    <DRUG_VOLUME></DRUG_VOLUME>
</RESUSCITATION_INFUSION_DRUG>

我想加入PATIENT_WEIGHT_CATEGORY字段中的两个xml文件。

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您有直接的XML数据,请不要使用数据集。这是VB.NET优于C#恕我直言的一个领域。

  Dim Table1 = XDocument.Load("path to file")...<rootElement>...<rowElement>

  Dim Table2 = XDocument.Load("path to file")...<rootElement>...<rowElement>


  Dim result = Table1.Join(inner:=Table2,

                        outerKeySelector:=Function(o) o.Key,

                        innerKeySelector:=Function(i) i.Key,

                        resultSelector:=Function(i,o) With New {.fieldName:= o.@f1,

                                                                .fieldName2:= i.@f2})

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

您还可以做的另一件事是将xml转换为expando对象并仍然执行连接操作。

即。
      Dim Table1 = XDocument.Load(“paty to the file”)...... .Select(Function(r as XElement)                          Dim记录为Object = New Dynamic.ExpandoObject()                          record.FieldName = r。@ Field.Value())

如果您提供实际架构,我可以提供更好的示例。

祝你好运