将英语维基百科转储导入SQL Server

将英语维基百科转储导入SQL Server,第1张

英语维基百科转储导入SQL Server

使用以下

SQL查询创建数据库

Create Database Feed;GOUSE [Feed]drop table Docdrop table links;GOCREATE TABLE [dbo].[Doc](   DocID int primary key,   Title [varchar](50) NULL,   URL [varchar](50) NULL,   Abstract [varchar](50) NULL)CREATE TABLE links(   DocID int,    linkType [varchar](10) NULL,   Anchor [varchar](50) NULL,    link [varchar](50) NULL   ConSTRAINT FK_DocID FOREIGN KEY (DocID)        REFERENCES dbo.Doc (DocID))

C#代码从xml填充数据库

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;using System.Data;using System.Data.SqlClient;namespace ConsoleApplication1{    class Program    {        const string FILENAME = @"c:temptest.xml";        const string CONNECTION_STRING = @"Server=.SQLEXPRESS;Database=Feed;Trusted_Connection=True;";        const string INSERT_DOC = "INSERT INTO [Feed].[dbo].[Doc] (" + "DocID, Title , URL, Abstract)" + " VALUES " + "(@DocID, @Title, @URL, @Abstract)";        const string INSERT_link = "INSERT INTO [Feed].[dbo].[links] (" + "DocID, linkType , Anchor, link)" + " VALUES " + "(@DocID, @linktype, @Anchor, @link)";        static void Main(string[] args)        { SqlConnection conn = new SqlConnection(CONNECTION_STRING); conn.Open(); SqlCommand docCmd = new SqlCommand(INSERT_DOC, conn); docCmd.Parameters.Add("@DocID", SqlDbType.Int); docCmd.Parameters.Add("@Title", SqlDbType.VarChar, 50); docCmd.Parameters.Add("@URL", SqlDbType.VarChar, 50); docCmd.Parameters.Add("@Abstract", SqlDbType.VarChar, 50); SqlCommand linksCmd = new SqlCommand(INSERT_link, conn); linksCmd.Parameters.Add("@DocID", SqlDbType.Int); linksCmd.Parameters.Add("@linkType", SqlDbType.VarChar, 10); linksCmd.Parameters.Add("@Anchor", SqlDbType.VarChar, 50); linksCmd.Parameters.Add("@link", SqlDbType.VarChar, 50); XmlReader reader = XmlReader.Create(FILENAME); int id = 0; while (!reader.EOF) {     if (reader.Name != "doc")     {         reader.ReadToFollowing("doc");     }     if (!reader.EOF)     {         XElement doc = (XElement)XElement.ReadFrom(reader);         id++;         docCmd.Parameters["@DocID"].Value = id;         docCmd.Parameters["@Title"].Value = (string)doc.Element("title");         docCmd.Parameters["@URL"].Value = (string)doc.Element("url");         docCmd.Parameters["@Abstract"].Value = (string)doc.Element("abstract");         int docRowsChanged = docCmd.ExecuteNonQuery();         foreach (XElement sublink in doc.Descendants("sublink"))         {  linksCmd.Parameters["@DocID"].Value = id;  linksCmd.Parameters["@linkType"].Value = (string)sublink.Attribute("linktype");  linksCmd.Parameters["@Anchor"].Value = (string)sublink.Element("anchor");  linksCmd.Parameters["@link"].Value = (string)sublink.Element("link");  int linksRowsChanged = linksCmd.ExecuteNonQuery();         }     } }        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5640450.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存