in

System.Data.SQLite

An open-source, enhanced version of the SQLite database engine for Windows

Change Encoding

Last post 02-13-2008 2:00 PM by Sam_. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 02-12-2008 2:01 PM

    • Matthias
    • Top 500 Contributor
    • Joined on 02-11-2008
    • Hamburg, Germany
    • Posts 2

    Change Encoding

    Hallo, I am using a database which is created/updated by a third party vendor. He seems to use a different encoding for string. Unfortunately SQLite.NET only supports UTF8 and UFT16. After digging into the source code for five minutes I found that the UT8 case uses the Encoding class, which could be changed. Although the code is not public so I am using reflection ;-)
      SQLiteConnection connection = new SQLiteConnection();
      SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
      builder.DataSource = "database.sq3";;
    
      connection.ConnectionString = builder.ConnectionString;
      connection.Open();
    
      FieldInfo fieldInfo = connection.GetType().GetField( "_sql", BindingFlags.Instance | BindingFlags.NonPublic );
      SQLiteConvert convert = (SQLiteConvert)fieldInfo.GetValue( connection );
      fieldInfo = convert.GetType().BaseType.BaseType.GetField( "_utf8", BindingFlags.Static | BindingFlags.NonPublic| BindingFlags.FlattenHierarchy );
      fieldInfo.SetValue( convert, Encoding.Default );
    
    After that the strings come out fine.
    I suggest to support other encodings in the connection string. Simply convert the encoding name to the proper Encoding class with Encoding.GetEncoding( name ).

    btw: It is allowd to insert pure html into the postings. Great way to inject some bad iframes or java script code... :-(
    Other forums use pseudo tags (i.e. ) and convert allowed tags to real tags.
  • 02-12-2008 2:36 PM In reply to

    • Sam_
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Washington DC
    • Posts 78

    Re: Change Encoding

    There's a "UseUTF16Encoding" option on the connection string.  Look in the help under SQLiteConnection class documentation.

    HTH,

    Sam 

    We're hiring! B-Line Medical is seeking .NET Developers for exciting positions in medical product development in MD/DC. Work with a variety of technologies in a relaxed team environment. See ads on http://careerbuilder.com.
  • 02-13-2008 1:21 PM In reply to

    • Matthias
    • Top 500 Contributor
    • Joined on 02-11-2008
    • Hamburg, Germany
    • Posts 2

    Re: Change Encoding

    The point was not to change between UTF8 and UTF16, but to change the enconding to something else, which the connection string unfortunately does not support.

    Regards,
    Matthias

  • 02-13-2008 2:00 PM In reply to

    • Sam_
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Washington DC
    • Posts 78

    Re: Change Encoding

     sqlite only supports UTF8 and UTF16 encodings.

    We're hiring! B-Line Medical is seeking .NET Developers for exciting positions in medical product development in MD/DC. Work with a variety of technologies in a relaxed team environment. See ads on http://careerbuilder.com.
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems