积极答复者
怎么向数据库的字段添加超长字符串?

问题
-
在开发一个带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
答案
-
关于linq和sql的数据类型映射关系请参考以下链接:
http://msdn.microsoft.com/zh-cn/library/Bb386947(v=vs.90).aspx#DefaultTypeMapping
可以看出System.String对应的是NVARCHAR(4000),所以如果字符串长度超过了4000,就会保存失败。
如果是很长的字符串,应该显式指定DBType,比如,把DBType设置成Text,具体请参考以下链接:
http://msdn.microsoft.com/zh-cn/library/Bb386926(v=vs.90).aspx?cs-save-lang=1&cs-lang=csharp
- 已标记为答案 Leo (Apple) Yang 2014年5月8日 1:52
2014年5月4日 3:07 -
需要添加数据注解(Data Annotation),也就是在这个ChapterContent属性上面添加[DataType(DataType.Text)],注意要添加对system.componentmodel.dataannotations命名空间
- 已标记为答案 Leo (Apple) Yang 2014年5月8日 1:52
2014年5月5日 7:52
全部回复
-
关于linq和sql的数据类型映射关系请参考以下链接:
http://msdn.microsoft.com/zh-cn/library/Bb386947(v=vs.90).aspx#DefaultTypeMapping
可以看出System.String对应的是NVARCHAR(4000),所以如果字符串长度超过了4000,就会保存失败。
如果是很长的字符串,应该显式指定DBType,比如,把DBType设置成Text,具体请参考以下链接:
http://msdn.microsoft.com/zh-cn/library/Bb386926(v=vs.90).aspx?cs-save-lang=1&cs-lang=csharp
- 已标记为答案 Leo (Apple) Yang 2014年5月8日 1:52
2014年5月4日 3:07 -
需要添加数据注解(Data Annotation),也就是在这个ChapterContent属性上面添加[DataType(DataType.Text)],注意要添加对system.componentmodel.dataannotations命名空间
- 已标记为答案 Leo (Apple) Yang 2014年5月8日 1:52
2014年5月5日 7:52 -
根据你的解释,思路大致明白了。不过我试了一下,遇到点问题,还是没办法解决。
private string _chapterContent; [Column(DbType = "TEXT")] public string ChapterContent { get { return _chapterContent; } set { if (_chapterContent != value) { NotifyPropertyChanging("ChapterContent"); _chapterContent = value; NotifyPropertyChanged("ChapterContent"); } } }
我在ChapterContent字段添加了关于类型的说明,DbType = "TEXT",但是运行的时候会提示,未知的数据类型而异常出错。不知道这里具体要设置为什么数据类型。
2014年5月9日 5:58 -
根据你的提示,我试了一下,直接加上
private string _chapterContent; [Column]
[DataType(DataType.Text)]
public string ChapterContent { get { return _chapterContent; } set { if (_chapterContent != value) { NotifyPropertyChanging("ChapterContent"); _chapterContent = value; NotifyPropertyChanged("ChapterContent"); } } }不过添加数据的时候,还是跟原来的一样,一加数据,就出错。
- 已编辑 yc wang 2014年5月9日 6:01
2014年5月9日 6:01