Personal Technology Information |
How to Use SQLXML to Retrieve Data from SQL Server Database?
Using SQL Server 2000 and above versions you can retrieve data from SQL Server in XML format directly from the database. Many XML related features were incorporated in the SQL Server database. This enables the development of XML enabled applications very easy and it makes exchange of data over internet easy. In this article we will discuss about how to use SQLXML to retrieve data directly from SQL server database in XML format. There are some SQL commands and clauses that help you to retrieve data from the SQL Server in the XML format. Syntax for such is given below: SELECT ... FOR XML mode [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] There are different modes and that take the values of · AUTO - In this mode every row of the result of the query forms an element in the returned XML document. · EXPLICIT - This mode defines how to map the columns of the results returned by the query to the XML elements and the attributes. · RAW - All the fields of the result are identified as attributes of an element. · NESTED - The result of this mode is the same as the AUTO mode but the formatting has to be done at the client side. A simple example of using the above syntax is given below: SELECT TOP 3 *FROM (SELECT FirstName+' '+LastName as Name FROM Employees) FOR XML AUTO The query produces this result: Employees Name="John Peter" There are optional parameters in the 'FOR XML mode' clause. The XMLDATA option adds an XSD schema which describes the XML format of the result returned. The ELEMENTS options make the fields of the tables in the database to be returned as child elements. If the ELEMENTS option is not there the fields of the tables are returned as attributes instead of child elements of the XML document. The ELEMENTS option is allowed only in the AUTO mode. The BINARY BASE64 option is used to retrieve any binary data. The returned binary data is returned in BASE64 encoded format. The binary data can be retrieved in the RAW or EXPLICIT mode. An example of the RAW mode is given below: SELECT EmpName, EmpCity from EMP for XML RAW The above code returns a result like, row EmpName="John" EmpCity="New York" In the above result you can note that the fields of the resultset are returned as attributes of the element. .Net provides with managed classes for interacting with SQL Server. The managed classes of SQLXML provided by .Net are: · SqlXmlCommand - This is used to perform queries that are stored in XML documents which are also called XML templates. This class also provides support for client side XML processing. · SqlXmlParameter - this is used to provide parameterized values and is used along with SqlXmlCommand object. · SqlXmlAdapter - this is the adapter class for SQLXML provider. This is used to fill the dataset. · SqlXmlException - this is used to trap the error information from the SQL Server. A sample code using the managed classes is given below. For detailed information on the usage of the above managed classes you can refer to the MSDN documentation. static string cstr = "Provider=SQLOLEDB;Server=(local);database=EmpPersonal;";public static void testingSqlXml (){ Stream oStr = Console.OpenStandardOutput(); SqlXmlCommand sqlcmd = new SqlXmlCommand(cstr); sqlcmd.Root = "Employees"; sqlcmd.CommandType = SqlXmlCommandType.Sql; sqlcmd.CommandText = "SELECT EmpName, EmpCity FROM Employees FOR XML AUTO"; strm = sqlcmd.ExecuteToStream(oStr); oStr.Close();} Thus we have seen that SQLXML can be used to retrieve data directly from the SQL Server in XML format. It is easy to handle and transmit data through the internet if it is in the XML format and this can be easily achieved using the SQLXML. Want to stay current with the latest technology developments realted to XML. Visit Free XML Tutorial to get your FREE subscription now! ** Attention Webmasters / Website Owners ** You can reprint this article on your website as long as you do not modify any of the content, and include our resource box as listed above with all links intact and hyperlinked properly.
MORE RESOURCES: Unable to open RSS Feed $XMLfilename with error HTTP ERROR: 403, exiting |
RELATED ARTICLES
How To Remove One Operating System from a Dual Boot Windows XP Computer In Windows Xp, you can install two operating systems on the same hard disk of one computer.Whether you have two copies of Windows Xp, or two different operating systems, then you end up with dual boot machine when you restart your Windows Xp computer. Review of Rio MP3 Players Below you will find some useful information and comments about a few of the most popular MP3 players by Rio, including the Nitrus, Carbon, Cali, and Forge. None of these MP3 players are perfect, but each offers a unique set of strengths and weaknesses that should be taken into consideration before you purchase any one of them. How to Switch to Firefox and Why You should First things first, what is Firefox? Well, it's a browser. Ok but what's that? A browser is a computer program used to view web pages, to browse through the world wide web. Selecting a Personal Digital Assistant A Computer in Your HandCarrying around an address book and planner is becoming a thing of the past. With new PDA developments, you can manage your contacts and schedule, use e-mail, and even listen to music. Can Movie Theatres Compete with Home HD TV in the Future? The battle is heating up for market share, home movie theatre, X-Box, Playstations and virtual reality take on the big screen and theme parks. You might be surprised to learn that the war is just beginning. USB Drives - What to Look For in a USB Device Those small USB drives have so many names, pocket drives, thumb drives, USB Pen, Jump drives, and several other names, but when it comes down to it, we are all talking about a small portable, reusable storage media that fits in the palm of your hand!There are a couple major factors you should consider when you select a USB drive. The size of the storage media you require, and the size and shape of the device itself. CCNA 640-801 Certification Primer The Cisco Certified Network Associate (CCNA) Certification is meant for career enhancement as well as gaining knowledge of the LAN/WAN technologies currently available for implementation. Hence, CCNA Certification can be obtained by IT professionals who have been a part of the industry for a considerable time and are interested in furthering their career in terms of role enhancement and salary. 10 Tips to Stay Safe and Secure Online The Internet can be a dangerous place.While you're enjoying the convenience of online shopping, Internet banking and subscription websites, nasty people lurk around every corner. Getting Started with HTML The basic language of the internet is html. If you can do crossword puzzles, if you can write a report for your teacher or boss, you can be creating web pages in a matter of minutes. Home Electronics: The Facts About Plasma TV Not so many years ago, homes across the country watched their favorite TV shows on a bulky floor model that took awhile to warm up before you could see the picture, didn't offer anything in the way of remote control manipulation and offered a washed out image on the TV's cathode ray tubehosted screen.. Basic Problem in a PC I have a p3 500MHz PC with 128MB RAM, 10.2 GB HDD Sony CD ROM 48x with window ME edition operating system. Bluetooth Headphones For Your PDA Nothing is worse than having to negotiate all kinds of cables with your many electronic devices. This is why bluetooth technology is so fantastic. A Peek Into the Near Future of Electronics Technology How long do you think DVDs have around? 20 years? 10 years? Actually, they have only been around for about seven years, but it seems like they have been around much longer. Many of us can hardly remember life before DVDs. Customize Your Portable Player with an MP3 Player Accessory Everywhere you look today people are listening to personal audio players. And many look for ways to personalize and/or enhance their experience. SOBIG.F Virus Promises Ill Be Back On 21 August 2003 Symantec Security Response upgraded the W32.SOBIG. SmartCar Memory Stick for LapTop Transfer Data With the new technology used to transfer information to from your digital camera to your laptop or PC using memory sticks which hold significant amounts of data it appears such technology will be perfect for net-centric cars. Where all the data is stored in a black box situation as many cars now have these features already. Is Your Web Browser Putting You At Risk? It's free, it comes with Windows and it's used by approximately 94% of the Internet population. It also poses an unacceptable security risk. Digital Cameras: How Many Pixels Do I Need? With the bewildering number of digital cameras on the market, it's increasing difficult to know where to start for your first purchase. One of the major determining factors of the price of a digital camera is the number of pixels. The Advantages of Portable MP3 Players With their solid-state technology, compact size, and abundant memory, portable mp3 players are the state of the art in portable music systems.Most portable mp3 players feature storage systems that have no moving parts. Seven Things to Consider When Choosing a PDA In the early days, Personal Digital Assistants (PDAs) were not much more than glorified calculators with the ability to store contact information and brief notes. Now, the line between personal computer and personal digital assistant is blurred thanks to the advanced capabilities of these useful little devices. |
home | site map | contact us |