/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlClient.GetXmlDataReader/CS/source.cs
74 строки
3 KB
Genevieve Warren
ropc fixes for ado.net (#42498)
07 сен 2024, 02:05
Не верифицирован
07 сен 2024, 02:05
cdcd7c4
Код
Авторство
О чём код?
using System; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Xml; static class Class1 { static void Main() { const string c = "my secure connection string"; GetXmlData(c); Console.ReadLine(); } // <Snippet1> // Example assumes the following directives: // using System.Data.SqlClient; // using System.Xml; // using System.Data.SqlTypes; static void GetXmlData(string connectionString) { using (SqlConnection connection = new(connectionString)) { connection.Open(); // The query includes two specific customers for simplicity's // sake. A more realistic approach would use a parameter // for the CustomerID criteria. The example selects two rows // in order to demonstrate reading first from one row to // another, then from one node to another within the xml column. const string commandText = "SELECT Demographics from Sales.Store WHERE " + "CustomerID = 3 OR CustomerID = 4"; SqlCommand commandSales = new(commandText, connection); SqlDataReader salesReaderData = commandSales.ExecuteReader(); // Multiple rows are returned by the SELECT, so each row // is read and an XmlReader (an xml data type) is set to the // value of its first (and only) column. var countRow = 1; while (salesReaderData.Read()) // Must use GetSqlXml here to get a SqlXml type. // GetValue returns a string instead of SqlXml. { SqlXml salesXML = salesReaderData.GetSqlXml(0); XmlReader salesReaderXml = salesXML.CreateReader(); Console.WriteLine("-----Row " + countRow + "-----"); // Move to the root. salesReaderXml.MoveToContent(); // We know each node type is either Element or Text. // All elements within the root are string values. // For this simple example, no elements are empty. while (salesReaderXml.Read()) { if (salesReaderXml.NodeType == XmlNodeType.Element) { var elementLocalName = salesReaderXml.LocalName; salesReaderXml.Read(); Console.WriteLine(elementLocalName + ": " + salesReaderXml.Value); } } countRow++; } } } // </Snippet1> }