我希望你能帮我处理一下使用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管理员的数据库中,该字符串将被保存,并且随后可以正常查看。
我尝试切换“UseUTF16Encoding=True;”True / False-没有任何区别。
您有什么想法我做错了什么。