Cannot insert certain characters in SQL Server

  • Thread starter jcsnippets.atspace.com
  • Start date
J

jcsnippets.atspace.com

Hi everyone,

Recently I have posted a question regarding special characters in text
files. I was trying to read the text file to process the text later on, but
I was using the wrong encoding. The correct encoding turned out to be
extended ASCII (850).

But now I'd like to store the result in a database. I have tried to do this
by just issueing an insert statement, using the following piece of code:
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
int result = cmd.ExecuteNonQuery();

This works, but now I'm faced with the same problem, only in reverse - ni
the database, the characters are stored in the wrong encoding!

Is there a way to specify the encoding in which I would like to safe the
characters in the database? What do I need to change in the code?

Thanks in advance,

JayCee
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

If you change the field to a Unicode field, e.g. ntext or nvarchar, it
can handle the characters without problem.
 
J

jcsnippets.atspace.com

If you change the field to a Unicode field, e.g. ntext or nvarchar, it
can handle the characters without problem.

Alas, the field I'm using already is nvarchar.

If I print the insert statement to the console window prior to passing it
to the database, the characters are correct. After the insert, they no
longer appear correct in the database (nor in the application that uses
said database).

Thanks,

JayCee
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

What do you use to insert the record? If you build the SQL query
yourself, remember that you have to put an N in front of a string
constant to make it Unicode.
 
J

jcsnippets.atspace.com

What do you use to insert the record? If you build the SQL query
yourself, remember that you have to put an N in front of a string
constant to make it Unicode.

Ah, I was not aware of that. I'm indeed building the SQL query myself, and
now that the N is in front of each String constant, the result in the
database looks fine.

Thank you very much!

Best regards,

JayCee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top