ASP
  Home arrow ASP arrow Make SQL Queries over HTTP with XML with V...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Make SQL Queries over HTTP with XML with VS.NET
By: Lati M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2002-04-05

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    This users guide will go through some of the basic steps that one might take in order send an HTTP request to a SQL Server. The request will instruct the SQL server to return XML, which we will parse and display neatly in a grid.RequirementsTo do this, we'll need SQLXML installed on top of SQL Server 2000. We'll also make use of the WebForm and XMLp components included in the IP*Works! .Net Edition. Click here to download ...This users guide will go through some of the basic steps that one might take in order send an HTTP request to a SQL Server. The request will instruct the SQL server to return XML, which we will parse and display neatly in a grid.
    Requirements
    To do this, we'll need SQLXML installed on top of SQL Server 2000. We'll also make use of the WebForm and XMLp components included in the IP*Works! .Net Edition. Click here to download a fully functional free trial of this very useful toolkit. For the purposes of this document, we will assume that IP*Works! .Net Edition has already been installed on the readers computer. This toolkit includes sample code for both VB.Net and C#, but here we will be coding in VB.Net.The first thing we need to do is understand how to make the http request to the SQL server. The best resource for this information is of course the help documentation for SQLXML itself. Using this documentation, we learn that we must run a SQLXML configuration application to configure SQLXML with IIS, creating a virtual directory. After that we'll see that the request is sent just like any other HTTP request, on port 80, with a query string containing the SQL query, ie:


    "SELECT CustomerId, CompanyName FROM Customer"

    http://localhost/sql?sql=select%20CustomerId,%20CompanyName%20from%20Customers%20FOR%20XML%20AUTO

    The resulting response is basic unvalidated XML which includes the results of the query. Try typing this in your browser to see the output for yourself (replace http://localhost/sql with your own sql virtual directory).Lets go ahead and get some GUI out of the way. We'll provide only a very few basic inputs for this demo. Textboxes for the URL to use to access the server, a query string, a user name, and password.I have changed the security settings of the IIS virtual directory to use basic authentication rather than windows integrated. NTLM authentication will be supported in the very soon to be released (entering Beta in Spr 2002) IP*Works! .Net SSL Edition.I am using the user name "sa" (system administrator) and the password "" (blank). The "Query" button will simply submit the HTTP request to the server using the WebForm component, receive the information back and parse it to the listview box on the form.[bold]WebForm[/bold]We'll use the WebForm component (rather than HTTP) because it can easily and automatically URL encode the query string information.In order to perform Basic Authentication, set the User and Password properties of the component, ie:


    Webform1.User txtUser.Text
    Webform1
    .Password txtPassword.Text

    Next, I set the encoding property of the WebForm component. This property allows me to select between query string (get), multipart/form data (post), or url encoded (post). Then we'll add the variables required in the query string using the AddFormVar method (syntax: AddFormVar(varname, varvalue)). As we see from the SQLXML documentation, the name of the variable is "sql" and the value of that variable is the query that we actually want to send. Then we'll add "FOR XML AUTO;" to instruct the server how to format the data (for more info on data formatting see the SQLXML documentation).


    Webform1.Encoding nsoftware.IPWorks.WebformEncodings.encQueryString Webform1.AddFormVar("sql"txtQuery.Text " FOR XML AUTO;"
    Webform1.SubmitTo(txtServer.Text

    Now the request has been received by the server and the server sends back the XML response. In order to catch this response, we can use either the Webform Transfer event, or the TransferredData property. Right now, we will use the TransferredData property. This property is ready and available as soon as the SubmitTo method returns.[bold]Parsing[/bold]Ok, so we have the XML data - now we will use whatever method to parse this xml and display it in a grid or listview. I am going to demonstrate using the XMLp component to parse this data and split it into a listview. We will dynamically create as many listview columns as there are search terms. For example, in my example we are searching for CustomerId and CompanyName - so there will be two columns in the resulting listview - CustomerId and CompanyName.The XML data that comes back from this request will not be valid, but raw XML with no parent nodes. We'll just add our own parent node quite simply by prepending and appending a root element around the data.


    'input the xml data received to the xml parser component 
    '
    (add "<root>" elementSQLServer does not provide validated XML
    Xmlp1.InputText "<root>" Webform1.TransferredData "</root>" 
    lvwOutput.Items.Clear() 
    lvwOutput.Columns.Clear() 
    lvwOutput.Columns.Add("Number"50HorizontalAlignment.Left

    'go to the first element and see what type of data we are getting, 
    '
    set up the appropriate column headers 
    Dim i 
    As Integer 
    Dim j 
    As Integer 
    Xmlp1
    .XPath "/root/[1]"  '[1] is the first child
    For i = 1 To Xmlp1.AttrCount 
         '
    For every attribute of this first childwe create a matching listview column
         lvwOutput
    .Columns.Add(Xmlp1.AttrName(i), (lvwOutput.Width 50) / Xmlp1.AttrCountHorizontalAlignment.Left
         
    lvwOutput.Columns(lvwOutput.Columns.Count 1).Text Xmlp1.AttrName(i
    Next i 

    'Now, go back to the root
    Xmlp1.XPath = "/root" 
    '
    And for every single child of the rootextract the info and add it to the listview
    For 1 To Xmlp1.XChildren 
         Xmlp1
    .XPath "/root/[" CStr(i) & "]" 
         
    lvwOutput.Items.Add(CStr(i)) 
         For 
    1 To Xmlp1.AttrCount 
              lvwOutput
    .Items(lvwOutput.Items.Count 1).SubItems.Add(Xmlp1.AttrValue(j)) 
         
    Next j 
    Next i

    There are of course many, many ways to parse the data - and many forms that your data might arrive in depending on the query you use. Use any query you like, parse the data any way you prefer. But this simple demonstration shows one easy way to perform SQL queries over HTTP using XML - after you get past the first steps the possibilities are many.[bold]More Information[/bold]For information about the author, please contact lancer@nsoftware.com.For more information about IP*Works! or the WebForm or XMLp components, please visit /n software.Copyright © 2002, Lance Robinson - All Rights Reserved. For publishing permissions, contact lancer@nsoftware.com.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP Articles
    More By Lati M

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! BlammoSplat: Build a community Web site of OpenLaszlo animations, Part 3: The community animation

    Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z

    IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects.
    FREE! Go There Now!


    NEW! IBM Rational Systems Development e-Kit

    As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br />
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! Software Change and Configuration Management Solution Guidelines

    This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Process

    Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly.
    FREE! Go There Now!


    NEW! Using IBM Rational Developer for System z and IBM Rational ClearCase together to manage application development

    Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes.
    FREE! Go There Now!


    NEW! Webcast: WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP ARTICLES

    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application
    - Easy Error Management





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT