本文介绍了SQL Server数据库中的NTEXT字段无法正确插入西里尔字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有表文章和Web API控制器CRUD操作。 文章{id:int,summary:nvarchar,text:ntext} 如果我使用t-sql插入数据,如 insert into Articles values(1,N'текст',N'ещемноготекста') 所有工作正常 - 数据库中的数据是可以的 - 所有的俄罗斯符号看起来都不错。 但是,如果我使用由问号填写的ajax字段text插入数据:'? ?? ????? ' $。post(http:// localhost:1000 / api / articles, { id:1,摘要:текст,文本:ещемноготекста} ).always(function(res) {console.log(res)}); 我的错误在哪里? PS :connectionString如果需要: < add name =DefaultConnectionconnectionString =Data Source = xxx; Initial Catalog = yyy ; User ID = zzz; Password = ***;的providerName = System.Data.SqlClient的/> PPS:我正在使用WebAPI中的deafult自动生成POST操作: // POST:api / articles [ResponseType(typeof(Article))] public IHttpActionResult PostArticle(article article) { if(!ModelState.IsValid) { return BadRequest(ModelState); } db.Articles.Add(article); db.SaveChanges(); return CreatedAtRoute(DefaultApi,new {id = article.ArticleId},article); } UPD: [DataContract] public class Article { [DataMember] public int Id {get;组; [DataMember] public string Summary {get;组; } [DataMember] [Column(TypeName =ntext)] public string Text {get;组; } 解决方案问题是你当您要使用EF Code First映射 NTEXT 列时,必须非常明确:您必须非常明确,并将列配置为所需类型,但您还必须将其配置为使用Unicode。为此,您可以使用Fluent API配置,例如使用以下代码覆盖 OnModelCreating(): modelBuilder .Entity< Aticle>()//选择要配置的实体 .Property(c => c.Text)//选择属性配置 .HasColumnType(NTEXT)//指定数据库类型 .IsUnicode(true); //指定Unicode(对于EF内部生成的参数) 然而,难怪有一个问题 NTEXT 。从 ntext,text和image(Transact-SQL) docs: ntext ,文本和图片数据类型将被删除Microsoft SQL Server的未来版本。 避免在新开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。使用nvarchar(max),varchar(max)和varbinary(max)。 How can I insert cyrillic symbols into my database?I have table Articles and Web API controller with CRUD operations.Articles { id: int, summary: nvarchar, text: ntext }If I insert data using t-sql likeinsert into Articles values (1, N'текст', N'еще много текста')all works fine - data in database is ok - all russian symbols looks good.But if I insert data using ajax field 'text' filled by question marks: '??? ????? ??????'$.post("http://localhost:1000/api/articles", { id: 1, summary: "текст", text: "еще много текста" }).always(function(res) {console.log(res)} );Where is my mistake?PS: connectionString if it need:<add name="DefaultConnection" connectionString="Data Source=xxx;Initial Catalog=yyy;User ID=zzz;Password=***;" providerName="System.Data.SqlClient"/>PPS: I'm using deafult autogenerated POST action from WebAPI:// POST: api/Articles[ResponseType(typeof(Article))]public IHttpActionResult PostArticle(Article article){ if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Articles.Add(article); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = article.ArticleId }, article);}UPD:[DataContract]public class Article{ [DataMember] public int Id { get; set; } [DataMember] public string Summary { get; set; } [DataMember] [Column(TypeName = "ntext")] public string Text { get; set; }} 解决方案 The problem is that you must be very explicit when you want to map NTEXT columns with EF Code First: you must be very explicit, and configure the column to be of the desired type, but you must also configure it to use Unicode. To do so you can use the Fluent API configuration, for example overriding the OnModelCreating() with a code like this:modelBuilder .Entity<Aticle>() // Choose the entity to configure .Property(c=> c.Text) // Choose the property to configure .HasColumnType("NTEXT") // Specify the DB type .IsUnicode(true); // Specify Unicode (for EF internally generated params)However, no wonder there is a problem with NTEXT. From ntext, text, and image (Transact-SQL) docs: ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. 这篇关于SQL Server数据库中的NTEXT字段无法正确插入西里尔字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 19:39