سلام بر برنامه نویسان عزیز 😉 در این مطلب کد تبدیل نتیجه کوئری به XML به وسیله SqlCommand و ExecuteXmlReader در سی شارپ #C را آماده کردم. انشاالله که این کد براتون مفید باشه.
کد تبدیل نتیجه کوئری به XML به وسیله SqlCommand و ExecuteXmlReader در سی شارپ #C :
using System;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
class MainClass {
public static void Main(string[] args)
{
using (SqlConnection con = new SqlConnection()) {
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
using (SqlCommand com = con.CreateCommand()) {
com.CommandType = CommandType.Text;
com.CommandText = "SELECT ID, FirstName" +
" FROM Employee FOR XML AUTO";
con.Open();
using (XmlReader reader = com.ExecuteXmlReader())
{
while (reader.Read())
{
Console.Write("Element: " + reader.Name);
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
using System.Xml;
using System.Data;
using System.Data.SqlClient;
class MainClass {
public static void Main(string[] args)
{
using (SqlConnection con = new SqlConnection()) {
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
using (SqlCommand com = con.CreateCommand()) {
com.CommandType = CommandType.Text;
com.CommandText = "SELECT ID, FirstName" +
" FROM Employee FOR XML AUTO";
con.Open();
using (XmlReader reader = com.ExecuteXmlReader())
{
while (reader.Read())
{
Console.Write("Element: " + reader.Name);
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{