0

附上上面的代码

<?xml version="1.0" encoding="UTF-8"?>
<QUERIES>
  <QUERY ID="XLIGR">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM XLINS.DWXF347 QUERY WHERE QUERY.GRP = 'XLIGR' UNION SELECT trim(CanadaQUERY.CDKEY1) as Product, 
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE CanadaQUERY.GRP = 'XLESCAN'
  </QUERY>
  <QUERY ID="XLIE">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM XLINS.DWXF347 QUERY WHERE (QUERY.GRP = 'XLDATA' OR QUERY.GRP='MIDMARKET')UNION SELECT trim(CanadaQUERY.CDKEY1) as Product,
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date 
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE (CanadaQUERY.GRP = 'XLESCAN' OR CanadaQUERY.GRP='CANDATA')
  </QUERY>
  <QUERY ID="XLES">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM XLINS.DWXF347 QUERY WHERE QUERY.GRP = 'XLES' UNION SELECT trim(CanadaQUERY.CDKEY1) as Product, 
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date 
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE CanadaQUERY.GRP = 'XLESCAN'
  </QUERY>
  <QUERY ID="XLIN">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM INCOMING.DWXF347 QUERY WHERE QUERY.GRP = 'INCOMING' UNION SELECT trim(CanadaQUERY.CDKEY1) as Product, 
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date 
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE CanadaQUERY.GRP = 'XLESCAN'
  </QUERY>
  <QUERY ID="XLSP">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM XLINS.DWXF347 QUERY WHERE QUERY.GRP = 'XLSELPROF' UNION SELECT trim(CanadaQUERY.CDKEY1) as Product, 
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date 
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE CanadaQUERY.GRP = 'XLESCAN'
  </QUERY>
  <QUERY ID="XLCN">
      SELECT trim(QUERY.CDKEY1) as Product, trim(QUERY.CDKEY2) as QUERY, trim(QUERY.CDDESC) as Description,QUERY.EFFDTE as Effective_Date,
      QUERY.ENDDTE as Expire_Date FROM XLINS.DWXF347 QUERY WHERE QUERY.GRP = 'CONSTRUCT' UNION SELECT trim(CanadaQUERY.CDKEY1) as Product, 
      trim(CanadaQUERY.CDKEY2) as QUERY, trim(CanadaQUERY.CDDESC) as Description,CanadaQUERY.EFFDTE as Effective_Date,CanadaQUERY.ENDDTE as Expire_Date 
      FROM XLCANADA.DWXF347 CanadaQUERY WHERE CanadaQUERY.GRP = 'XLESCAN'
  </QUERY>
<QUERIES>

我需要阅读内联并在 VB.net 中执行,然后使用 c# 代码调用它。我尝试过硬编码并在 vb .net 中执行查询并获得结果集作为数据集

private string GetSymbol(String argQueryID)
        {
            string l_Query;
            XmlDocument l_XmlDoc = new XmlDocument();
            //load the xml file 
            l_XmlDoc.Load(Server.MapPath("~/XML/Queries.config"));
            l_Query = l_XmlDoc.SelectSingleNode("/QUERIES/QUERY[@ID='" + argQueryID + "']").InnerText;
            return l_Query;
        }
4

1 回答 1

0

您可以创建与您的 xml 文件相对应的类,并使用序列化/反序列化来读取文件并迭代或选择您的查询。

模型:

<Xml.Serialization.XmlRoot("QUERIES")>
Public Class Queries
    <Xml.Serialization.XmlElement("QUERY")>
    Public Property Queries As List(Of Query)
    <Xml.Serialization.XmlIgnore>
    Default Public ReadOnly Property Index(ID As String) As String
        Get
            Return Queries.SingleOrDefault(Function(q) q.ID = ID)?.CommandText
        End Get
    End Property
End Class

Public Class Query
    <Xml.Serialization.XmlAttribute>
    Public Property ID As String
    <Xml.Serialization.XmlText>
    Public Property CommandText As String
End Class

测试代码:

Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Queries))
Dim myQueries As Queries ' will hold your query objects
Using sr As New IO.StreamReader("filename.xml")
    myQueries = DirectCast(mySerializer.Deserialize(sr), Queries)
End Using
For Each myQuery In myQueries.Queries ' iterate over query objects
    Console.WriteLine($"Query ID={myQuery.ID}, CommandText='{myQuery.CommandText}'")
Next
Dim myCommandText = myQueries("XLSP") ' get the query with ID XLSP
Console.WriteLine($"XLSP CommandText='{myCommandText}'")
myCommandText = myQueries("ABCD") ' get a query which doesn't exist (returns "")
Console.WriteLine($"ABCD CommandText='{myCommandText}'")

现在您有了查询,您可以在数据库中执行它们

于 2019-07-29T15:55:29.487 回答