0

I'm querying a SQL server database using the following code, and getting some unexpected results back. Some of my characters are getting converted to '?' characters. The code I am using to query is:

var theConnectionString = @"Data Source=my-sql-server;Initial Catalog=My_Database;Integrated Security=True";
var theQuery = @"select '❤' as panda";

var dataSet = new DataSet();
using (SqlConnection connection = new    SqlConnection(theConnectionString))
{
    connection.Open();
    var command = new SqlCommand(theQuery, connection);
    foreach (var param in parameters.Keys)
    {
        command.Parameters.AddWithValue(param, parameters[param]);
    }
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    adapter.Fill(dataSet);
}
return dataSet;

This results in a dataset of 1 column with 1 row, with a value of '?' (character code ), instead of 10084.

Interactive debugging shows me that the values are correctly encoded in theQuery though.

Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
  • You have a column name called ❤ ? It sounds really fun `:D` – Soner Gönül Jul 01 '13 at 14:40
  • 2
    How about `select N'❤' as panda`? – LittleBobbyTables - Au Revoir Jul 01 '13 at 14:43
  • @SonerGönül Sadly not, that's an example static query I was using I'm selecting the value ❤ as column 'panda'. Neither pandas or ❤s are essential concepts within the technology stack I am using. Which is a shame. – Ceilingfish Jul 01 '13 at 14:44
  • @LittleBobbyTables that's spot on. What is that doing? Can you turn that into an answer? – Ceilingfish Jul 01 '13 at 14:46
  • 1
    @Ceilingfish - it's treating the string as Unicode/`nvarchar`. See [this question](http://stackoverflow.com/questions/10025032/what-is-the-meaning-of-the-prefix-n-in-t-sql-statements) here for a better answer. I feel like this has to be a duplicate question, which is why I hesitated to write a full answer. – LittleBobbyTables - Au Revoir Jul 01 '13 at 14:49

0 Answers0