Wp插入到db异常中

时间:2013-02-13 10:02:05

标签: c# database exception windows-phone

我的代码工作了一个多月,我的应用解析rss feed(http://www.whitehouse.gov/feed/blog/white-house)并将新闻插入db:

今天当app试图将此新闻“2013年国情咨询中的第一夫人的盒子”添加到数据库时,我遇到了一个例外。这是我的代码:

News item = Query.instance().AddNews(channel.Guid, channel.Description, channel.Link, channel.PublishDate, channel.Title);

   public News AddNews(string guid, string description, string link, DateTime publishDate, string title)
    {
        // create a new and add it to the context
        News item = new News { Guid = guid, Description = description, Link = link, PublishDate = publishDate, Title = title };
        // add the new to the context
        db.NewsItems.InsertOnSubmit(item);
        // save changes to the database
        db.SubmitChanges();

        return item;
    }

我做了一个调试,问题在于对新闻的描述(似乎是长篇),这里有例外:

  

“发生了'System.InvalidOperationException'类型的异常   Microsoft.Phone.Data.Internal.ni.dll并没有在a之前处理过   托管/本地边界类型的第一次机会异常   发生'System.InvalidOperationException'   System.Data.Linq.ni.dll“

这是db

中的列描述
private string _description;
    [Column]
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            if (_description != value)
            {
                NotifyPropertyChanging("Description");
                _description = value;
                // Remove HTML tags. 
                _description = Regex.Replace(_description, "<[^>]+>", string.Empty);
                // Remove newline characters
                _description = _description.Replace("\r", "").Replace("\n", "");
                // Remove encoded HTML characters
                _description = HttpUtility.HtmlDecode(_description);
                //replace spaces
                _description = _description.Replace("  ", "");

                //if (!string.IsNullOrEmpty(_description) && _description.Length > 3900)
                //    _description = _description.Substring(0, 3900);

                NotifyPropertyChanged("Description");
            }
        }
    }

当我取消注释时,它起作用:

//if (!string.IsNullOrEmpty(_description) && _description.Length > 3900)
//    _description = _description.Substring(0, 3900);

2 个答案:

答案 0 :(得分:1)

我们需要异常的主体来帮助您解决问题。 但我认为(正如Emiliano Magliocca所说),问题在于DB中的Description单元格可以在您尝试插入时保留较少的字符。您可以修改它将行描述更改为varchar(max)或Text。无论如何提供身体的激活,然后我们会帮助你。

Asnwer:

您应该将列描述的数据类型更改为Varchar(max),然后您可以随意将任意数量的文本保存到此列,因为varchar(max)最多可以容纳2GB的文本。当您使用codeFirst方法生成表时,请使用以下属性: [Column(TypeName =&#34; varchar(MAX)&#34;)] public string Description ...

而不是 [柱] public string Description ...

答案 1 :(得分:0)

感谢Maris建议我找到了正确的解决方案:

[Column(DbType = "ntext")] 
public string Description

而不是

[Column(TypeName = "varchar(MAX)")] 
public string Description

第二个不适用于Windows手机;)

相关问题