在c#中添加SQL Server中的数据时出错?

时间:2021-01-09 15:52:23
StringBuilder Description = new StringBuilder();
        StringBuilder Title = new StringBuilder();
        StringBuilder URL = new StringBuilder();
        SqlConnection cs = new SqlConnection("Data Source = MyPC\\SQLEXPRESS; Initial Catalog = Youtube; Integrated Security =True;");
        SqlDataAdapter da = new SqlDataAdapter();

        string url = TextBox1.Text.ToString();
        var doc = XDocument.Load(url);
        var items = doc.Descendants("item");

        XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";
        XNamespace nsfeedburner = "http://rssnamespace.org/feedburner/ext/1.0";


        cs.Open();
        foreach (var item in items)
        {
            Title.Append( item.Element("title").Value); //For Title
            var encodedContent = (string)item.Element(nsContent + "encoded");
            var decodedContent = System.Net.WebUtility.HtmlDecode(encodedContent); 
            var html = new HtmlDocument();
            html.LoadHtml(decodedContent);
            var ps = html.DocumentNode.Descendants("p");

            foreach (var p in ps)
            {
                var textContent = p.InnerText;
                Description.Append(textContent.Trim().ToString());
            }

           URL.Append((string)item.Element(nsfeedburner + "origLink"));

          da.InsertCommand = new SqlCommand("INSERT INTO datalinks VALUES (@URL, @Title, @Content)", cs);
            da.InsertCommand.Parameters.Add(new SqlParameter("@URL", SqlDbType.VarChar,250)).Value = URL.ToString();
            da.InsertCommand.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar, 200)).Value = Title.ToString();
            da.InsertCommand.Parameters.Add(new SqlParameter("@Content", SqlDbType.VarChar, 2000)).Value = Description.ToString();


            da.InsertCommand.ExecuteNonQuery();

        }
        cs.Close();

I am using this code to insert some fetched values in my database from : http://feeds.feedburner.com/TechCrunch

我使用此代码从我的数据库中插入一些获取的值:http://feeds.feedburner.com/TechCrunch

The content at the source have 20 different data (urls, titles, descriptions etc.)

来源的内容有20个不同的数据(网址,标题,描述等)

Its working fine but its adding the first data values in all the 20 rows every time.

它的工作正常但每次都在20行中添加第一个数据值。

What it sould really do is fetch and add all the different 20 rows of (urls, titles, descriptions) but its adding 1st row all the time. (20 times)

它真正做的是获取并添加所有不同的20行(网址,标题,描述),但它一直添加第一行。 (20次)

please help Thanks

请帮忙谢谢

1 个解决方案

#1


3  

Check to see that the field order in the table matches the order you are inserting in. Or add the field names as part of the input statement

检查表中的字段顺序是否与您要插入的顺序相匹配。或者将字段名称添加为输入语句的一部分

#1


3  

Check to see that the field order in the table matches the order you are inserting in. Or add the field names as part of the input statement

检查表中的字段顺序是否与您要插入的顺序相匹配。或者将字段名称添加为输入语句的一部分