c# - Wp插入数据库异常

标签 c# database exception windows-phone

我的代码运行了一个多月,我的应用程序解析 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;
    }

我进行了调试,问题出在新闻的描述上(看起来很长),这里是异常(exception):

"An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.Data.Internal.ni.dll and wasn't handled before a managed/native boundary A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.ni.dll"

这是数据库中的列描述

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);

最佳答案

我们需要异常的正文来帮助您解决问题。 但我认为(正如 Emiliano Magliocca 所说),问题在于您在数据库中的描述单元格可以容纳比您尝试插入的字符更少的字符。您可以修复它,将行描述的类型更改为 varchar(max) 或文本。无论如何,请提供异常的正文,然后我们会帮助您。

回答:

您应该将 Description 列的数据类型更改为 Varchar(max),然后您可以随意将任意数量的文本保存到该列,因为 varchar(max) 最多可以容纳 2gb 的文本。当您使用 codeFirst 方式生成表时,请使用如下属性: [列(TypeName = "varchar(MAX)")] 公共(public)字符串描述...

代替 [柱子] 公共(public)字符串描述...

关于c# - Wp插入数据库异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14850861/

相关文章:

Java - 当外部类捕获异常时进行监听

exception - 返回 Exception 实例而不是在 Python 中引发它有什么缺点?

java - 我在 android 上使用 java 时遇到异常 (java.lang.NoClassDefFoundError),为什么?

c# - 元胞自动机的实现

javascript - 使用或不使用正则表达式在 C# 中查找并替换多个字符串

php - 我如何在我的数据库中配对图像?

c# - 自引用实体 - 多对多

sql - 使用 join 进行高级 sql 查询

C# - 从 ZIP 中提取特定目录,保留文件夹结构

c# - 如何遵循方法的作用?