asp.net中怎么调用sqlxml中的template中的查询数据

sojust 2004-10-27 09:49:25
asp.net中怎么调用sqlxml中的template中的查询数据呢

我在网上找了很久都没找到相关资料,希望哪位仁兄帮个忙,如果有这方面的资料发上来,先谢了!!

另外,我想问问大家在得出sqlserver中数据如果应用xml的呢??
...全文
117 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
marvelstack 2004-12-17
  • 打赏
  • 举报
回复
Executing Template Files by Using the CommandText Property
This example illustrates how template files that consist of SQL or XPath queries can be specified by using the CommandText property. Instead of specifying the SQL or XPath query as the value of CommandText, you can specify a file name as the value. In the following example, the CommandType property is specified as SqlXmlCommandType.TemplateFile.

The sample application executes this template:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:query>
SELECT top 2 CustomerID, CompanyName
FROM Customers
FOR XML AUTO
</sql:query>
</ROOT>
This is the C# sample application. To test the application, save the template (TemplateFile.xml) and then execute the application.

Note In the code, you must provide the name of the instance of Microsoft® SQL Server™ in the connection string.

using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
static string NorthwindConnString = "Provider=SQLOLEDB;Server=(local);database=Northwind;Integrated Security=SSPI";

public static int testParams()
{
//Stream strm;
SqlXmlCommand cmd = new SqlXmlCommand(NorthwindConnString);
cmd.CommandType = SqlXmlCommandType.TemplateFile;
cmd.CommandText = "TemplateFile.xml";
using (Stream strm = cmd.ExecuteStream()){
using (StreamReader sr = new StreamReader(strm)){
Console.WriteLine(sr.ReadToEnd());
}
}

return 0;
}
public static int Main(String[] args)
{
testParams();
return 0;
}
}
To test the application

To test this example, you must have the Microsoft .NET Framework installed on your computer.

Save the XML template (TemplateFile.xml) that is provided in this example in a folder.


Save the C# code (DocSample.cs) that is provided in this example in the same folder in which the schema is stored. (If you store the files in a different folder, you will have to edit the code and specify the appropriate directory path for the mapping schema.)


Compile the code. To compile the code at the command prompt, use:
csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
This creates an executable (DocSample.exe).

At the command prompt, execute DocSample.exe.


If you pass a parameter to a template, the parameter name must begin with at sign (@); for example, p.Name="@EmployeeID", where p is a SqlXmlParameter object.
This is the updated template which takes one parameter.

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:header>
<sql:param name='CustomerID'>ALFKI</sql:param>
</sql:header>
<sql:query>
SELECT CustomerID, CompanyName
FROM Customers
WHERE CustomerID=@CustomerID
FOR XML AUTO
</sql:query>
</ROOT>
This is the updated code in which a parameter is passed in to execute the template.

public static int testParams()
{

Stream strm;
SqlXmlParameter p;

SqlXmlCommand cmd = new SqlXmlCommand(NorthwindConnString);
cmd.CommandType = SqlXmlCommandType.TemplateFile;
cmd.CommandText = "TemplateFile.xml";
p = cmd.CreateParameter();
p.Name="@CustomerID";
p.Value = "ANATR";
strm = cmd.ExecuteStream();
StreamReader sw = new StreamReader(strm);
Console.WriteLine(sw.ReadToEnd());
return 0;
}
sojust 2004-10-27
  • 打赏
  • 举报
回复
没人回答吗,up一下都沉底了:(
sojust 2004-10-27
  • 打赏
  • 举报
回复
如何结合 sqlserver和xml使用的?

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧