Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Azure SQL Database
Dit voorbeeld illustreert hoe XPath-queries worden uitgevoerd tegen een mapping-schema.
Beschouw dit schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="Con" sql:relation="Person.Contact" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FName"
sql:field="FirstName"
type="xsd:string" />
<xsd:element name="LName"
sql:field="LastName"
type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="ContactID" type="xsd:integer" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
Deze C#-applicatie voert een XPath-query uit tegen dit schema (MySchema.xml).
Opmerking
In de code moet je de naam van de instantie van Microsoft SQL Server in de verbindingsstring opgeven.
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 testXPath()
{
Stream strm;
SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
cmd.CommandText = "Con";
cmd.CommandType = SqlXmlCommandType.XPath;
cmd.RootTag = "ROOT";
cmd.SchemaPath = "MySchema.xml";
strm = cmd.ExecuteStream();
using (StreamReader sr = new StreamReader(strm)){
Console.WriteLine(sr.ReadToEnd());
}
return 0;
}
public static int Main(String[] args)
{
testXPath();
return 0;
}
}
De toepassing testen
Zorg ervoor dat je het Microsoft .NET Framework op je computer hebt geïnstalleerd.
Sla het XSD-schema (MySchema.xml) dat in dit voorbeeld wordt gegeven op in een map.
Sla de C#-code (DocSample.cs) die in dit voorbeeld wordt gegeven op in dezelfde map waarin het schema is opgeslagen. (Als je de bestanden in een andere map opslaat, moet je de code bewerken en het juiste directorypad voor het mappingschema opgeven.)
Compileer de code. Om de code in de opdrachtprompt te compileren, gebruik u:
csc /reference:Microsoft.Data.SqlXML.dll DocSample.csDit creëert een uitvoerbaar bestand (DocSample.exe).
Voer bij de opdrachtprompt DocSample.exeuit.