欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#怎样才能将XML文件导入SQL Server

程序员文章站 2023-11-24 14:38:22
问:怎样才能将xml文件导入sql server 2000? 答:将xml文件导入sql server有若干种方法,这里提供其中的3种: 大容量装载com接口。如果需要将文...

:怎样才能将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);
}
}