none
怎么向数据库的字段添加超长字符串? RRS feed

  • 问题

  • 在开发一个带sdf数据库功能的应用,其中有个表里的ChapterContent字段是一大篇文章,字数有可能会上万。发现添加数据库记录的时候,会出现异常,而添加失败,尝试把赋给ChapterContent的内容减小一点,马上就添加成功了。

    所以想知道一下,有没有办法向字段直接添加非常大的字符串?是不是不能用string数据类型,而改用别的什么数据类型?


                ContentItem item = new ContentItem();
                item.ChapterId = ViewModel.AllContentItems.Count + 1;
                item.ChapterContent = textBox1.Text;
                ViewModel.AddContentItem(item);
    

    下面是用到的类和方法:

    ContentItem类:

        [Table]
        public class ContentItem : INotifyPropertyChanged, INotifyPropertyChanging
        {
            private int _id;
            [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
            public int Id
            {
                get { return _id; }
                set
                {
                    if (_id != value)
                    {
                        NotifyPropertyChanging("Id");
                        _id = value;
                        NotifyPropertyChanged("Id");
                    }
                }
            }
    
            private int _chapterId;
            [Column]
            public int ChapterId
            {
                get { return _chapterId; }
                set
                {
                    if (_chapterId != value)
                    {
                        NotifyPropertyChanging("ChapterId");
                        _chapterId = value;
                        NotifyPropertyChanged("ChapterId");
                    }
                }
            }
    
            private string _chapterContent;
            [Column]
            public string ChapterContent
            {
                get { return _chapterContent; }
                set
                {
                    if (_chapterContent != value)
                    {
                        NotifyPropertyChanging("ChapterContent");
                        _chapterContent = value;
                        NotifyPropertyChanged("ChapterContent");
                    }
                }
            }
    
            #region INotifyPropertyChanged Members
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            // Used to notify that a property changed
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            #endregion
    
            #region INotifyPropertyChanging Members
    
            public event PropertyChangingEventHandler PropertyChanging;
    
            // Used to notify that a property is about to change
            private void NotifyPropertyChanging(string propertyName)
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
                }
            }
    
            #endregion
        }

    AddContentItem方法:

            public void AddContentItem(ContentItem item)
            {
                try
                {
                    Db.ContentItems.InsertOnSubmit(item);
                    Db.SubmitChanges();
                }
                catch
                {
                    Debug.Assert(false);
                }
            }


    2014年5月3日 8:50

答案

全部回复