Unicode字符串到SQLite数据库中

我想在Visual Studio C#代码中提供一些帮助,将unicode字符串插入到SQLite数据库中。

下面是我将测试字符串写入数据库的测试代码:

string testStr = "á Á ñ ç é á"; SQLiteConnection mydataConnection = new SQLiteConnection(); // setup new sql connection obj try { //// SQLite DB mydataConnection.ConnectionString = "Data Source=C:\\Users\\John\\Desktop\\location.db; Version=3; UseUTF16Encoding=True;Synchronous=Normal;New=False"; // set up the connection string mydataConnection.Open(); // open the connection to the db SQLiteCommand myCmd = new SQLiteCommand(); // create a new command object myCmd.Connection = mydataConnection; // whats its connected to, see above connection string SQLiteParameterCollection myParameters = myCmd.Parameters; // good pratice to use parameters to pass data to db myParameters.AddWithValue("@name", testStr); // myCmd.CommandText = "INSERT INTO location (name) VALUES (@name)"; myCmd.ExecuteNonQuery(); } catch (SQLiteException d) { string myerror = "Database Error" + d; MessageBox.Show(myerror); } finally // good pratice to close db connection in a finally so exceptions dont leave open. { mydataConnection.Close(); } 

当我查看数据库/表(使用SQLite管理员)时,字符串看起来像这样:áÃÃÃÃÃÃÃÃÃá

作为测试,我可以使用SQLite Administrator将字符串直接复制并粘贴到数据库中,并保存字符串,随后可以查看确定。

我试过切换“UseUTF16Encoding = True;” 真/假 – 没有区别。

我有什么想法我做错了。

这个问题结果是我用来查看/检查数据库/数据的SQLite管理员应用程序的问题。 似乎是这个应用程序在我的代码插入时无法正确显示字符。 奇怪的是,如果您使用SQLite Administrator应用程序直接添加测试文本(通过将测试字符串复制并粘贴到表/字段中),它将显示,保存并随后查看确定。 无论如何现在使用SourceForge SQLite数据库浏览器检查我的代码正确写入,所有似乎都按顺序。

非常感谢任何花时间发表评论的人,希望这对其他人有所帮助。

您可以尝试使用此代码

 byte[] encod = Encoding.Default.GetBytes(testStr ); string result = Encoding.UTF8.GetString(encod); myParameters.AddWithValue("@name", result);