我是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>
答案 0 :(得分:2)
最简单的方法是使用FOR XML RAW
在SQL中执行此操作。
SELECT [occ], [date], [time], [field], [good]
FROM [tbl_Data]
WHERE ([occ] = @occ)
FOR XML RAW
有关详细信息,请参阅this article。