我需要一些关于将 unicode 字符串插入 SQLite 数据库的 Visual Studio C# 代码方面的帮助。
下面是我将测试字符串写入数据库的测试代码:
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 Administrator)时,字符串如下所示:
à à à à à à
作为测试,我可以使用 SQLite Administrator 将字符串直接复制并粘贴到数据库中,然后保存该字符串,然后可以正常查看。
我尝试切换“UseUTF16Encoding=True;”对/错 - 没有区别。
任何想法我做错了什么。
这个问题原来是我用来查看/检查数据库/数据的 SQLite Administrator 应用程序的问题。似乎是这个应用程序在我的代码插入时无法正确显示字符。奇怪的是,如果您使用 SQLite Administrator 应用程序直接添加测试文本(通过将测试字符串复制并粘贴到表/字段中),它将显示、保存并随后查看“确定”。无论如何,现在使用 SourceForge SQLite 数据库浏览器来检查我的代码是否正确写入,并且一切似乎都按顺序进行。
非常感谢任何花时间发表评论的人,希望这对其他人有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)