示例#1
0
        private static void ExecuteXmlReaderExample(XmlFormatOption xmlFormatOption = XmlFormatOption.Auto)
        // Чтение набора в формате XML
        {
            DbConnection connection = DatabaseProvider.GetDatabaseConnection(NorthwindConnectionStringName);
            string       @select    = string.Format("SELECT ContactName, CompanyName FROM Customers {0}",
                                                    xmlFormatOption.ToOperator());

            try
            {
                connection.Open();
                var       sqlCommand = new SqlCommand(select, connection as SqlConnection);
                XmlReader xmlReader  = sqlCommand.ExecuteXmlReader();
                xmlReader.Read();
                string data;
                do
                {
                    data = xmlReader.ReadOuterXml();
                    if (!string.IsNullOrEmpty(data))
                    {
                        Console.WriteLine(data);
                    }
                } while (!string.IsNullOrEmpty(data));
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
示例#2
0
        public static string ToOperator(this XmlFormatOption xmlFormatOption)
        {
            switch (xmlFormatOption)
            {
            case XmlFormatOption.Auto:
                return("FOR XML AUTO");

            //case XmlFormatOption.Explicit:
            //   return "FOR XML EXPLICIT";
            case XmlFormatOption.Raw:
                return("FOR XML RAW");

            default:
                throw new NotSupportedException(string.Format("Not supported option: {0}", xmlFormatOption));
            }
        }