C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to write an XML doccument from database query


Posted Date: 15 Jun 2006    Resource Type: Articles    Category: .NET Framework
Author: uma maheswariMember Level: Bronze    
Rating: Points: 7



<1. How to write an XML document from a database query?

The following code will write an XML document named myXMLdata.xml in the directory of the source file. In this example, we want to run a SQL query to the Products table of the SQL Server 7.0 Northwind database, and construct an XML document from the results of the query. At first we need to include two Namespaces: the System.Data for using the ADO.Net classes, and the System.Data.SQL for using the SQL Server managed provider. The System.IO and the System.Text namespaces are used for the FileStream and StringBuilder classes, respectively.

Building the SQL string is a common task in database applications. Since the String objects are immutable (that means, it cannot be copied or modified at the same memory location), a StringBuffer object is more desirable while extending long strings in several steps. Hence, a StringBuilder object has been used to build the SQL string. The remainder of the code is self-explanatory.

// MyDotNet\myCSharp\XML\ WriteXML.cs
// Author: Mesbah U. Ahmed
using System;
using System.Data;// for ADO.Net
using System.Data.SQL; // for SQL Server managed provider
using System.IO; // for FileStream class
using System.Text; // for StringBuilder class
public class WriteXML
{ public static void Main()
{ // Build a connection
SQLConnection myConn =
new SQLConnection
("server=yourServerName;uid=yourUserId;pwd=yourPassword;database=Northwind");
// Build the SQL string
StringBuilder mySql = new StringBuilder("SELECT ProductId, ");
mySql.Append("ProductName, UnitPrice ");
mySql.Append("FROM Products ");
mySql.Append("WHERE UnitPrice < 10.00");
mySql.Append("ORDER BY UnitPrice ");
Console.WriteLine(mySql);

// Build the DataSet Command object
SQLDataSetCommand myCmd = new SQLDataSetCommand
(mySql.ToString(), myConn);
// Build the DataSet
DataSet myDs = new DataSet();

// Fill the DataSet with a dataset table named "Products"
myCmd.FillDataSet(myDs, "Products");

// Get a FileStream object
FileStream myFs =
new FileStream
("myXmlData.xml",FileMode.OpenOrCreate,FileAccess.Write);

// Use the WriteXml method of DataSet object to write an XML file from the DataSet
myDs.WriteXml(myFs);
myFs.Close();
Console.WriteLine("File Written");
}
}

Compilation: While compiling the source code, we will need to reference the System.Data.dll and System.XML,dll assemblies as follows:
csc /r:System.Data.dll;System.XML.dll WriteXML.cs






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creation of Array Of Text Boxes at the runtime
Previous Resource: Move Files One system to another system
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use