我的代码工作了一个多月,我的应用程序解析 rss 提要(http://www.whitehouse.gov/feed/blog/white-house)并将新闻插入数据库:
今天,当应用程序尝试将这条新闻“2013 年国情咨文的第一夫人的盒子”添加到 db 时,我遇到了一个例外。这是我的代码:
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;
}
我进行了调试,问题在于新闻的描述(似乎很长),这是例外:
“在 Microsoft.Phone.Data.Internal.ni.dll 中发生‘System.InvalidOperationException’类型的异常,并且在托管/本机边界之前未处理 System.Data 中发生‘System.InvalidOperationException’类型的第一次机会异常.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);