• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

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

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

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

另外,我想问问大家在得出sqlserver中数据如果应用xml的呢??
...全文
89 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过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使用的?
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-10-27 09:49
社区公告
暂无公告