程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#怎樣才能將XML文件導入SQL Server

C#怎樣才能將XML文件導入SQL Server

編輯:C#基礎知識

:怎樣才能將XML文件導入SQL Server 2000?
:將XML文件導入SQL Server有若干種方法,這裡提供其中的3種:

大容量裝載COM接口。如果需要將文檔的實體和屬性析取到關系表中,最快的方法就是使用SQL Server 2000 Extensible Markup Language 3.0 Service Pack 1(SQLXML 3.0 SP1)提供的大容量裝載COM接口。大容量狀態COM接口包含在SQLXML 3.0 SP1的免費下載中。

textcopy.exe命令行實用工具。如果不希望將文檔的實體和屬性析取到關系表中,您可以使用textcopy.exe命令行實用工具。Textcopy.exe是將文本和image數據類型從單一服務器行或列移入或移出的優秀工具。

數據轉換服務(DTS)。如果XML文檔很簡單,您可以使用DTS將信息逐行析取到表中。這一方法要求您將XML文件定義為輸入數據源,將數據庫表定義為輸出數據源,並編寫ActiveX腳本剖析"<"和">"方式的字符輸入,以析取實體、屬性及其值。

要導入的Xml文件:
代碼如下:
 
<?xml version="1.0" encoding="gb2312"?>
<Tbl_UpdateLogs>
<Table>
<id>32</id>
<title>新增執法機構頁面</title>
<content>qqqqqq</content>
<module>組織機構</module>
<updateTime>2009-07-31T00:00:00+08:00</updateTime>
<operator>王永剛</operator>
</Table>
<Table>
<id>33</id>
<title>執法人員資格頁面</title>
<content>大幅度放到大幅度放到</content>
<module>組織機構</module>
<updateTime>2009-07-29T00:00:00+08:00</updateTime>
<operator>王永剛</operator>
</Table>
<Table>
<id>34</id>
<title>111111</title>
<content>dfdwdd</content>
<module>qwqwq</module>
<updateTime>2009-07-29T00:00:00+08:00</updateTime>
<operator>wyg</operator>
</Table>
<Table>
<id>35</id>
<title>qq</title>
<content>fjdldldsss</content>
<module>qqqqqqq</module>
<updateTime>2009-07-30T00:00:00+08:00</updateTime>
<operator>wyg</operator>
</Table>
<Table>
<id>36</id>
<title>2009222</title>
<content>ddddd</content>
<module>22</module>
<updateTime>2009-07-31T00:00:00+08:00</updateTime>
<operator>wyg</operator>
</Table>
<Table>
<id>37</id>
<title>1234455</title>
<content>ddddddd</content>
<module>11111</module>
<updateTime>2009-07-31T00:00:00+08:00</updateTime>
<operator>wyg</operator>
</Table>
</Tbl_UpdateLogs>
/// <summary>
/// 讀取Xml文件,獲得所有節點的Value值
/// </summary>
/// <param name="fileName">Xml文件名</param>
/// <param name="filePath">存放的路徑</param>
/// <param name="rootName">Xml根節點名稱</param>
/// <returns></returns>
public void ImportXmlFile(string fileName,string filePath,string rootName)
{
string loadPath = HttpContext.Current.Server.MapPath(filePath + fileName);
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(loadPath);
//獲取根節點<rootName>的所有子節點
XmlNodeList myNodeList = xmldoc.SelectSingleNode(rootName).ChildNodes;
//遍歷<根節點>的所有子節點
foreach (XmlNode myXmlNode in myNodeList)
{
XmlNodeList subNodeList = myXmlNode.ChildNodes;
UpdateLogs updateLog = new UpdateLogs();
foreach (XmlNode subXmlNode in subNodeList)
{
switch (subXmlNode.Name)
{
case "id": //節點的名稱,加這個條件是因為<!---->這些節點也會被讀出來
updateLog.ID = int.Parse(subXmlNode.InnerText.Trim());
break;
case "title":
updateLog.Title = subXmlNode.InnerText.Trim();
break;
case "content":
updateLog.Content = subXmlNode.InnerText.Trim();
break;
case "module":
updateLog.Module = subXmlNode.InnerText.Trim();
break;
case "updateTime":
updateLog.UpdateTime = DateTime.Parse(subXmlNode.InnerText.Trim());
break;
case "operator":
updateLog.Operator = subXmlNode.InnerText.Trim();
break;
default:
break;
}
}
if (isExstsById(updateLog.ID.ToString()))
{
UpdateLog(updateLog);
}
else
{
InsertLog(updateLog);
}
}
File.Delete(loadPath);
BindData();
ClientScript.RegisterStartupScript(typeof(string), "importLog", "<script>alert('導入成功!');</script>");
}
catch (Exception ex)
{
ExceptionManager.Handle(ex);
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved