title: Insert UTF8 data into a SQL Server 2008
body: <p>I have an issue with encoding. I want to put data from a UTF-8-encoded file into a SQL Server 2008 database. SQL Server only features UCS-2 encoding, so I decided to explicitly convert the retrieved data.</p>
  
  <pre><code>// connect to page file
  _fsPage = new FileStream(mySettings.filePage, FileMode.Open, FileAccess.Read);
  _streamPage = new StreamReader(_fsPage, System.Text.Encoding.UTF8);
  </code></pre>
  
  <p>Here's the conversion routine for the data:</p>
  
  <pre><code>private string ConvertTitle(string title)
  {
    string utf8_String = Regex.Replace(Regex.Replace(title, @"\\.", _myEvaluator), @"(?&lt;=[^\\])_", " ");
    byte[] utf8_bytes = System.Text.Encoding.UTF8.GetBytes(utf8_String);
    byte[] ucs2_bytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.Unicode, utf8_bytes);
    string ucs2_String = System.Text.Encoding.Unicode.GetString(ucs2_bytes);
  
    return ucs2_String;
  }
  </code></pre>
  
  <p>When stepping through the code for critical titles, variable watch shows the correct characters for both utf-8 and ucs-2 string. But in the database its - partially wrong. Some special chars are saved correctly, others not.</p>
  
  <ul>
  <li>Wrong: ń becomes an n</li>
  <li>Right: É or é are for example inserted correctly.</li>
  </ul>
  
  <p>Any idea where the problem might be and how to solve it?</p>
  
  <p>Thans in advance,
  Frank</p>