Use oledb objects to convert all tables in the database into XML files

Source: Internet
Author: User
ASP. NET dataset facilitates the use of XML files on the page. How can I convert all your data in a database? If your database is compatible with oledb, let's take a look at how to convert all the tables in the database into XML files. The file name is the same as the name of the table in the database. Ben Code It has two versions: C # and VB. NET.

C # version: [test]

<% @ Page Language = "C #" %>
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. oledb" %>
<SCRIPT runat = "server">
Void page_load (Object sender, eventargs e ){
If (! Ispostback ){
Textbox1.text = "provider = sqloledb; Data Source =.; initial catalog = pubs; user id = sa; Password = ;";
Label1.text = "";
}
}
Void createxml (Object sender, eventargs e ){
Oledbconnection dataconn = new oledbconnection (textbox1.text );
URI Path = request. url;
String serverurl = path. tostring ();
Serverurl = serverurl. substring (0, serverurl. lastindexof ("/") + 1 );
Try {
Literal1.text = "you have created a file in the same directory: <br/> ";
Dataconn. open ();
Datatable schematable = dataconn. getoledbschematable (oledbschemaguid. Tables, new object [] {null, "table "});
For (INT I = 0; I <schematable. Rows. Count; I ++ ){
Oledbdataadapter dbadapter = new oledbdataadapter ("select * from [" + schematable. Rows [I]. itemarray [2]. tostring () + "]", dataconn );
Dataset tabledata = new dataset ();
Dbadapter. Fill (tabledata, schematable. Rows [I]. itemarray [2]. tostring ());
Tabledata. writexml (httpcontext. Current. server. mappath (schematable. Rows [I]. itemarray [2]. tostring () + ". xml "));
Literal1.text = literal1.text + "<a href = '" + serverurl + schematable. Rows [I]. itemarray [2]. tostring () + ". xml'> ";
Literal1.text = literal1.text + schematable. Rows [I]. itemarray [2]. tostring () + ". xml </a> <br/> ";
}
}
Catch (exception ex ){
Label1.text = ex. Message. tostring ();
}
Finally {
Dataconn. Close ();
}
}
</SCRIPT>
<HTML>
<Head>
<Title> ledb2xml: Example of converting a database to an XML file </title>
</Head>
<Body bgcolor = "# ffffff">
<Center>
<H2> <font face = ""> oledb2xml: Example of converting a database to an XML file </font>
</H2>
</Center>
<Form runat = "server" id = "form1">
<Font face = "">
<P align = "center"> enter the oledb connection string and click "generate xml file. </P>
<P align = "center">
<Asp: textbox id = "textbox1" runat = "server" width = "600"> </ASP: textbox>
</P>
<P align = "center">
<Asp: button id = "button1" onclick = "createxml" runat = "server" text = "generate xml file"> </ASP: button>
</P>
<P align = "center">
<Asp: Label id = "label1" runat = "server" font-bold = "true" forecolor = "red"> </ASP: Label>
</P>
<P align = "center">
<Asp: literal id = "literal1" runat = "server"> </ASP: literal>
</P>
</Font>
</Form>
</Body>
</Html>

VB. NET version: [test]

<% @ Import namespace = "system. Data. oledb" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system" %>
<% @ Page Language = "VB" %>
<HTML>
<Head>
<Title> ledb2xml: Example of converting a database to an XML file </title>
<SCRIPT runat = "server">
Sub page_load (sender as object, e as eventargs)
If not ispostback then
'Textbox1. Text = "provider = sqloledb; Data Source =.; initial catalog = pubs; user id = sa; Password = ;"
Label1.text = ""
Textbox1.text = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("test. mdb ")
End if
End sub
Sub createxml (sender as object, e as eventargs)
Dim dataconn as new oledbconnection (textbox1.text)
Dim path as uri = request. url
Dim serverurl as string = path. tostring ()
Serverurl = serverurl. substring (0, serverurl. lastindexof ("/") + 1)
Try
Literal1.text = "you have created a file in the same directory: <br/>"
Dataconn. open ()
Dim schematable as datatable
Schematable = dataconn. getoledbschematable (oledbschemaguid. Tables, new object () {nothing, "table "})
Dim I as integer
For I = 0 to schematable. Rows. Count-1
Dim dbadapter as new oledbdataadapter ("select * from [" + schematable. Rows (I). itemarray (2). tostring () + "]", dataconn)
Dim tabledata as new dataset ()
Dbadapter. Fill (tabledata, schematable. Rows (I). itemarray (2). tostring ())
Tabledata. writexml (httpcontext. Current. server. mappath (schematable. Rows (I). itemarray (2). tostring () + ". xml ")))
Literal1.text = literal1.text + "<a href = '" + serverurl + schematable. Rows (I). itemarray (2). tostring () + ". xml'>"
Literal1.text = literal1.text + schematable. Rows (I). itemarray (2). tostring () + ". xml </a> <br/>"
Next I
Catch ex as exception
Label1.text = ex. Message. tostring ()
Finally
Dataconn. Close ()
End try
End sub
</SCRIPT>
</Head>
<Body bgcolor = "# ffffff">
<H2 align = center> <font face = ""> oledb2xml: Example of converting a database to an XML file </font> </H2>
<Form runat = "server" id = "form1">
<Font face = "">
<P align = "center"> enter the oledb connection string and click "generate xml file. </P>
<P align = "center">
<Asp: textbox id = "textbox1" runat = "server" width = "600"> </ASP: textbox>
</P>
<P align = "center">
<Asp: button id = "button1" onclick = "createxml" runat = "server" text = "generate xml file"> </ASP: button>
</P>
<P align = "center">
<Asp: Label id = "label1" runat = "server" font-bold = "true" forecolor = "red"> </ASP: Label>
</P>
<P align = "center">
<Asp: literal id = "literal1" runat = "server"> </ASP: literal>
</P>
</Font>
</Form>
</Body>
</Html>

For databases with large data volumes, processing may take more time. Therefore, you may need to change the responsedeadlockinterval attribute of processmodel in the machine. config file, for example, 5 minutes. The default value is 3 minutes.

enable = "true"
timeout = "infinite"
idletimeout = "infinite"
shutdowntimeout = "0:00:05"
requestlimit = "infinite"
requestqueuelimit = "5000"
restartqueuelimit = "10"
memorylimit = "60"
webgarden = "false"
cpumask = "0 xffffffff"
username = "machine"
Password = "autogenerate"
loglevel = "errors"
clientconnectedcheck = "0:00:05"
comauthenticationlevel = "Connect"
comimpersonationlevel = "Impersonate"
timeout = "00:09:00"
responsedeadlockinterval = "00:05:00" default is 00:03:00
maxworkerthreads = "25 "
maxiothreads =" 25 "/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.