Database Reference
In-Depth Information
Figure 15-15. The
Design view of the WriteXML form
5.
Double-click the empty surface of the
WriteXML.cs
form, and it will open the
code editor window, showing the
WriteXML _Load
event. Modify the
WriteXML
_Load
event to look like Listing 15-7.
Listing 15-7.
WriteXML.cs
Using System.Data.SqlClient;
private void btnXML_Click(object sender, EventArgs e)
{
// Connection string
string connString = @" server=.\sql2012; database=AdventureWorks;
Integrated Security=true";
// Query
string qry = @"select Name ,ProductNumber
from Production.Product";
// Create connection
SqlConnection conn = new SqlConnection(connString);
try
{
// Create Data Adapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry, conn);
// Open connection
conn.Open();
// Create and Fill Dataset
DataSet ds = new DataSet();
da.Fill(ds, "Production.Product");
// Extract data set to XML file
ds.WriteXml(@"c:\productstable.xml");
MessageBox.Show("The XML file is Created");
}
catch (Exception ex)
{