执行 SQL 查询(SQLXML 托管类)
本示例演示:
创建参数(SqlXmlParameter 对象)。
向 SqlXmlParameter 对象的属性(Name 和 Value)分配值。
在本示例中,将执行一个简单的 SQL 查询,以检索其姓氏值作为参数传递的雇员的名字、姓氏和出生日期。在指定参数 (LastName) 时,只设置 Value 属性。而不设置 Name 属性,因为在此查询中,参数是位置参数,不需要名称。
默认情况下,SqlXmlCommand 对象的 CommandType 属性为 Sql。因此,不显式设置此属性。
![]() |
---|
在代码中,必须在连接字符串中提供 Microsoft SQL Server 实例的名称。 |
以下是 C# 代码:
using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks;Integrated Security=SSPI";
public static int testParams()
{
Stream strm;
SqlXmlParameter p;
SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
cmd.CommandText = "SELECT FirstName, LastName FROM Person.Contact WHERE LastName=? For XML Auto";
p = cmd.CreateParameter();
p.Value = "Achong";
string strResult;
try
{
strm = cmd.ExecuteStream();
strm.Position = 0;
using(StreamReader sr = new StreamReader(strm))
{
Console.WriteLine(sr.ReadToEnd());
}
}
catch (SqlXmlException e)
{
//in case of an error, this prints error returned.
e.ErrorStream.Position=0;
strResult=new StreamReader(e.ErrorStream).ReadToEnd();
System.Console.WriteLine(strResult);
}
return 0;
}
public static int Main(String[] args)
{
testParams();
return 0;
}
}
测试应用程序
将本主题中提供的 C# 代码 (DocSample.cs) 保存在某个文件夹中。
编译代码。若要在命令提示符下编译此代码,请使用:
csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
这将创建一个可执行文件 (DocSample.exe)。
在命令提示符下,执行 DocSample.exe。
您可以指定一个模板(如下面的代码段所示),该模板执行 updategram(也是一个模板)来插入客户记录,而不是将 SQL 查询指定为命令文本。您可以在文件中指定模板和 updategram,然后执行文件。有关详细信息,请参阅使用 CommandText 属性执行模板文件。
SqlXmlCommand cmd = new SqlXmlCommand("Provider=SQLOLEDB;Data Source=SqlServerName;Initial Catalog=Database; Integrated Security=SSPI;");
Stream stm;
cmd.CommandType = SqlXmlCommandType.UpdateGram;
cmd.CommandText = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql' xmlns:updg='urn:schemas-microsoft-com:xml-updategram'>" +
"<updg:sync>" +
"<updg:before/>" +
"<updg:after>" +
"<Customer CustomerID='aaaaa' CustomerName='Some Name' CustomerTitle='SomeTitle' />" +
"</updg:after>" +
"</updg:sync>" +
"</ROOT>";
stm = cmd.ExecuteStream();
stm = null;
cmd = null;
使用 ExecuteToStream
如果您具有现有流,则可以使用 ExecuteToStream 方法,而不是创建 Stream 对象和使用 Execute 方法。此处修订前面示例中的代码以使用 ExecuteToStream 方法:
using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
static string ConnString = "Provider=SQLOLEDB;Server=SqlServerName;database=AdventureWorks;Integrated Security=SSPI;";
public static int testParams()
{
SqlXmlParameter p;
MemoryStream ms = new MemoryStream();
StreamReader sr = new StreamReader(ms);
ms.Position = 0;
SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
cmd.CommandText = "select FirstName, LastName from Person.Contact where LastName = ? For XML Auto";
p = cmd.CreateParameter();
p.Value = "Achong";
cmd.ExecuteToStream(ms);
ms.Position = 0;
Console.WriteLine(sr.ReadToEnd());
return 0;
}
public static int Main(String[] args)
{
testParams();
return 0;
}
}
![]() |
---|
还可以使用 ExecuteXMLReader方法,它将返回 XmlReader 对象。有关详细信息,请参阅使用 ExecuteXMLReader 方法执行 SQL 查询。 |