簡體   English   中英

需要從xml文件讀取內聯查詢並使用Vb.net執行

[英]Need to read inline query from xml file and execute using Vb.net

附加上面的代碼

<?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;
        }

您可以創建與您的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}'")

現在,您有了查詢,就可以在數據庫中執行查詢了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM