【问题标题】:ASPX SQL Query Result To XMLASPX SQL 查询结果到 XML
【发布时间】:2014-07-10 09:56:01
【问题描述】:

我是 asp.net 的新手,我想做的是获取 SQL 查询结果并将它们输出到 XML 字符串中。我在 ColdFusion 上做过这个,但我无法在 asp 中弄清楚。 非常感谢任何帮助。

谢谢

ASP 代码:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>    
</head>
<body>

<form id="form1" runat="server">
<div>    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DuplicationConnectionString %>" 

        SelectCommand="SELECT [occ], [date], [time], [field], [good] FROM [tbl_Data] WHERE ([occ] = @occ)">
        <SelectParameters>
            <asp:Parameter DefaultValue="AAA" Name="occ" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>    
</div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ocd" HeaderText="occ" SortExpression="occ" />
        <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
        <asp:BoundField DataField="time" HeaderText="time" SortExpression="time" />
        <asp:BoundField DataField="field" HeaderText="field" SortExpression="field" />
        <asp:BoundField DataField="good" HeaderText="good" SortExpression="good" />
    </Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Button" />
</form>

ColdFusion 代码:

<cfquery name="getData" datasource="Data_01" username="" password="">
    SELECT *
    FROM tbl_data       
</cfquery>

<cfxml variable="mydoc">
   <?xml version='1.0' encoding='utf-8'?><result><rows>
    <cfoutput query="getData">
        <row>                   
        <cell>#occ#</cell>
        <cell>#date#</cell>
        <cell>#time#</cell>
        <cell>#field#</cell>
        <cfif #good# is 0>
            <cell>No</cell>
        <cfelse>
            <cell>Yes</cell>
        </cfif>
        </row>
    </cfoutput>
       </rows></result>
</cfxml>

【问题讨论】:

  • CF提示:&lt;cfif #good# is 0&gt;&lt;cell&gt;No&lt;/cell&gt;&lt;cfelse&gt;&lt;cell&gt;Yes&lt;/cell&gt;&lt;/cfif&gt;可简写为&lt;cell&gt;#YesNoFormat(good)#&lt;/cell&gt;

标签: asp.net sql xml database coldfusion


【解决方案1】:

在 SQL 中使用FOR XML RAW 最容易做到这一点。

SELECT [occ], [date], [time], [field], [good] 
FROM [tbl_Data] 
WHERE ([occ] = @occ) 
FOR XML RAW

更多详情请参阅this article

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2013-03-25
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多