HOME > .NET Framework > Forum > database ทีมีอยู่ sql server 2005 ให้เป็น xml อย่างไรครับ รบกวนหน่อยครับ การทำ database เป็น xml ต้องทำอย่างไรครับ ช่วยหน่อยนะครับ
database ทีมีอยู่ sql server 2005 ให้เป็น xml อย่างไรครับ รบกวนหน่อยครับ การทำ database เป็น xml ต้องทำอย่างไรครับ ช่วยหน่อยนะครับ
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim strConnString,strSQL As String
strConnString = "Server=localhost;Uid=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "SELECT * FROM customer"
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
'*** Export XML ***'
ds.Tables(0).WriteXML("C:\XML\myXml.xml")
'ds.Tables(0).WriteXmlSchema("C:\XML\myXml.xml")
ds = Nothing
Code (C#)
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;Uid=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
strSQL = "SELECT * FROM customer";
objConn.ConnectionString = strConnString;
var _with1 = objCmd;
_with1.Connection = objConn;
_with1.CommandText = strSQL;
_with1.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dtAdapter = null;
objConn.Close();
objConn = null;
//*** Export XML ***'
ds.Tables[0].WriteXml("C:\\XML\\myXml.xml");
//ds.Tables(0).WriteXmlSchema("C:\XML\myXml.xml")
ds = null;
XmlDocument xmlDoc = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("CategoryList");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
// Create a new <Category> element and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("Category");
// Set attribute name and value!
parentNode.SetAttribute("ID", "01");
xmlDoc.DocumentElement.PrependChild(parentNode);
// Create the required nodes
XmlElement mainNode = xmlDoc.CreateElement("MainCategory");
XmlElement descNode = xmlDoc.CreateElement("Description");
XmlElement activeNode = xmlDoc.CreateElement("Active");
// retrieve the text
XmlText categoryText= xmlDoc.CreateTextNode("XML");
XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles.");
XmlText activeText = xmlDoc.CreateTextNode("true");
// append the nodes to the parentNode without the value
parentNode.AppendChild(mainNode);
parentNode.AppendChild(descNode);
parentNode.AppendChild(activeNode);
// save the value of the fields into the nodes
mainNode.AppendChild(categoryText);
descNode.AppendChild(descText);
activeNode.AppendChild(activeText);
// Save to the XML file
xmlDoc.Save("C:\\categories.xml);
Code (C#)
int FakeQuantity = 0;
XmlDocument Doc = new XmlDocument();
XmlAttribute newAtt = default(XmlAttribute);
XmlElement TempNode = default(XmlElement);
// Use the XmlDeclaration class to place the
// <?xml version="1.0"?> declaration at the top of our XML file
XmlDeclaration dec = Doc.CreateXmlDeclaration("1.0", null, null);
Doc.AppendChild(dec);
XmlElement DocRoot = Doc.CreateElement("Orders");
Doc.AppendChild(DocRoot);
// Generate a couple of phony orders
int x = 0;
for (x = 0; x <= 11; x++) {
XmlNode Order = Doc.CreateElement("Order");
newAtt = Doc.CreateAttribute("Quantity");
FakeQuantity = 10 * x + x;
newAtt.Value = FakeQuantity.ToString();
Order.Attributes.Append(newAtt);
DocRoot.AppendChild(Order);
}
Doc.Save("OutDocument.xml");
Code
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data