前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

操纵OleDb工具,实现将数据库中全数表转换成XML文件法度代码

时候:2011-05-31 19:53来源:知行网www.zhixing123.cn 编辑:麦田守望者

ASP.NET Dataset让你在页面中利用XML格局的文件供应了便利。如果您的数据全数都在一个数据库中,该若何进行转换呢?如果你的数据库与OleDb兼容的话,下面就看看若何把数据库中的所有表转换成XML文件文件名字与数据库中的表的名字不异。本代码包含两个版本:C#版本和VB.NET版本。

C#版本:

<%@ 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 = "已在你的不异目次下建立了一下文件:<br/>" ;
dataConn.Open();
DataTable schemaTable = dataConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, 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'>" + 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:数据库转换成XML文件例子</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h2><font face="宋体">OleDb2XML:数据库转换成XML文件例子</font>
</h2>
</center>
<form runat="server">
<font face="宋体">
<p align="center">请输入OleDb连接字符串,然后点“天生XML文件”按钮。</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="天生XML文件"></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版本:

<%@ Page Language="VB" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<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 = ""
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 = "已在你的不异目次下建立了一下文件:<br/>"
dataConn.Open()
Dim schemaTable As DataTable = dataConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, 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'>" + 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>

<html>
<head>
<title>leDb2XML:数据库转换成XML文件例子</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h2><font face="宋体">OleDb2XML:数据库转换成XML文件例子</font>
</h2>
</center>
<form runat="server">
<font face="宋体">
<p align="center">请输入OleDb连接字符串,然后点“天生XML文件”按钮。</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="天生XML文件"></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>


对数据量年夜的数据库,措置可能会需求更多的时候,是以有可能需求变动machine.config文件中processModel的responseDeadlockInterval 属性,比如为5分钟,默许为3分钟。
<processModel
enable="true"
timeout="Infinite"
idleTimeout="Infinite"
shutdownTimeout="0:00:05"
requestLimit="Infinite"
requestQueueLimit="5000"
restartQueueLimit="10"
memoryLimit="60"
webGarden="false"
cpuMask="0xffffffff"
userName="machine"
password="AutoGenerate"
logLevel="Errors"
clientConnectedCheck="0:00:05"
comAuthenticationLevel="Connect"
comImpersonationLevel="Impersonate"
responseRestartDeadlockInterval="00:09:00"
responseDeadlockInterval="00:05:00" default is 00:03:00
maxWorkerThreads="25"
maxIoThreads="25" />
 

------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码
------分开线----------------------------
保举内容
猜你感兴趣