3

LINQ と MS SQL Server に関して非常に厄介な問題があります。

Im currently making some software in WPF C# and use LINQ to generate classes from the data in the database.

However, when i update a nvarchar or varchar field in the DB from my program, LINQ adds trailing spaces to the string!

I have a field in a table defined like this:

ProductName = NVARCHAR(10)

So if i do this:

Product.Name = "Bike";
Product.Name = Product.Name.Trim();
repository.Add(Product);   // Runs an .InsertOnSubmit
repository.Save();         // Runs a .SubmitChanges

then the resulting data in the DB is "Bike[SPACE][SPACE][SPACE][SPACE][SPACE][SPACE]" where [SPACE] is just a space (can't write multiple spaces in this text here).

Why does it do this, and how do i make it stop adding those annoying trailing spaces?

Thanks in advance

4

3 に答える 3

6

私の記憶が正しければ、私は同じ問題を抱えていました...それはlinqが原因ではなく、列タイプが原因です。VARCHAR に変更してみてください。

于 2009-12-10T08:30:58.510 に答える
0

Ncharも同じものを作ります。

これらの型は空白を埋めます: Nchar と Nvarchar

データベースを変換し、モデルを更新しました。すべて修正。

それを確認します...

SQL Server の char、nchar、varchar、および nvarchar の違いは何ですか?

モデルを変更したくない場合は、mssql のトリム関数を使用できます。

于 2012-11-06T18:48:45.920 に答える