1

I want to insert a string with trademark symbol (™) to MSSQL 2008.

When I was searching the problem I found some SQL based solutions:

Trimming the symbol:

SELECT REPLACE('Microsoft™',char(153),'')

Insert only the symbol:

INSERT mytable(col1) VALUES (char(153))

Also I have read this question but I can't find a solution.

Is there any way to insert a word with the symbol (like 'Microsoft™') using Linq to SQL?

I made my trials on a nvarchar column.

Edit:

This is my table:

Sellers table

This is my mapping at dbml:

SellerName property

This is my code:

var db = new DBDataContext();

Seller seller;
if(!db.GetTable<Seller>().Any(s => s.Username == username))
{
    seller = new Seller {Username = username, SellerName = name, FeedbackCount = feedback};
    db.Sellers.InsertOnSubmit(seller);
    db.SubmitChanges();
}

This is what I want to insert as an name: B♥Râ™

And This is the generated SQL:

INSERT INTO [dbo].[Sellers]([Username], [SellerName], [FeedbackCount])
VALUES (@p0, @p1, @p2)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input VarChar (Size = 8000; Prec = 0; Scale = 0) [testuser]
-- @p1: Input NVarChar (Size = 4000; Prec = 0; Scale = 0) [B♥Râ™]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [886]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.17929
Community
  • 1
  • 1
pilavust
  • 538
  • 1
  • 7
  • 20
  • Which is the field that is causing problems? SellerName or Username ? Whichever it is, please can we see the generated c# for that property, including the attributes? (just click on the property you are interested in and hit f12) – Marc Gravell Jan 29 '13 at 19:52
  • Only using trademark symbol works! But i guess i have encoding problems. But I solved it according to [this](http://stackoverflow.com/questions/5723238/having-trouble-with-utf-8-storing-in-nvarchar-in-sql-server-2008) question. – pilavust Jan 30 '13 at 23:04

3 Answers3

2

You should be able to just insert it. C# strings are unicode as are nvarchar columns. Have you tried simply inserting it?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • He should check that the database is set to support Unicode and not just Latin1. – Dustin Kingen Jan 29 '13 at 18:07
  • I tried but i get the exception: `String or binary data would be truncated.\r\nThe statement has been terminated`. I double checked the column type: `nvarchar(32)`. My word sample is: `B♥Râ™` – pilavust Jan 29 '13 at 18:15
  • @Marc Gravell as I understand correctly this is a T-SQL demonstration. How can i do this with c#? – pilavust Jan 29 '13 at 18:21
  • Are you sure that the user name is the problem and not sellername? Try running SQL profiler too see the exact query sent to the server. – Anders Abel Jan 29 '13 at 18:48
  • @AndersAbel Thanks. Problem is SellerName. But unfortunately i am using sqlexpress and i have no sql profiler. – pilavust Jan 29 '13 at 19:05
  • If your problem is solved and this answer was the one helping you to solve it, please mark it as accepted by clicking the green check mark. – Anders Abel Jan 30 '13 at 09:29
1

Have you tried with the ASCII value ( Alt + 0153 ) for "tm" symbol?

G21
  • 1,297
  • 2
  • 18
  • 39
1

Your literals are not unicode; try:

SELECT REPLACE(N'Microsoft™',N'™','')

and

INSERT mytable(col1) VALUES (N'™')

(hint: they work)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • @pilavust if you are using LINQ-to-SQL then there shouldn't be a problem in the first place, as long as the columns / parameters etc are configured correctly. Try using `dbContext.Log` to see what the SQL is that is being used (or SQL trace). However! The examples you provide are not LINQ-to-SQL so it is hard to comment. – Marc Gravell Jan 29 '13 at 18:18
  • I cannot generate SQL so i checked the mapping. Type: `string (System.String)` and Server Data Type: `NVarChar(32)`. It looks normal to me. – pilavust Jan 29 '13 at 18:39
  • 1
    @pilavust LINQ-to-SQL generates SQL all the time; that is what it does. The .Log property allows you to see what is actually going over the wire. That or SQL Trace. Basically, to comment further, we need to see what is going over the wire/ – Marc Gravell Jan 29 '13 at 19:51
  • Sorry you are right. I added the generated sql to the question. – pilavust Jan 29 '13 at 20:16
  • @pilavust interesting; can you confirm `SellerName` is an `nvarchar` column? what ends up in that column? – Marc Gravell Jan 29 '13 at 20:40
  • yep double checked. I added the table and mapping screenshots to question. – pilavust Jan 29 '13 at 20:48
  • @pilavust and what is the data that you end up with? or is it an exception? – Marc Gravell Jan 29 '13 at 20:52
  • do you mean `Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.17929`? I don't know. I just copy the full log here. I get the log with this: `db.Log = new System.IO.StreamWriter("linq-to-sql.log", false, Encoding.Unicode) { AutoFlush = true };` – pilavust Jan 29 '13 at 20:56