[英]How to insert and retrieve data from sqlite database with correct character set?
我正在使用sqlite數據庫(通過ADO,System.Data.SQLite)在C#中編寫程序。
問題是,當我從c#應用程序中插入包含瑞典語或德語字符(åäöüÅÄÖÖÜ等)的字符串時,它們被錯誤地存儲在數據庫中。 (看起來像¶和Ãect),但是如果我使用SQLite Administrator將它們直接插入到數據庫中,它們將被正確保存。
此外,當我嘗試使用正確存儲的C#應用程序檢索數據時,它以相同的方式被弄亂了...
我究竟做錯了什么?
保存數據的代碼:
SQLiteConnection cnn = new SQLiteConnection("Data Source=C:\temp\database.s3db");
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = "INSERT INTO images (image_name, tags, relevance) VALUES (\"Example Image åäö.jpg\", \"test\", 3)";
mycommand.ExecuteNonQuery();
cnn.Close();
cnn.Dispose();
嘗試這個:
private IDbDataParameter AddParameter(IDbCommand command, string paramName, DbType type, object value)
{
IDbDataParameter parameter = command.CreateParameter();
parameter.ParameterName = paramName;
parameter.DbType = type;
if (value != null)
parameter.Value = value;
else
parameter.Value = DBNull.Value;
command.Parameters.Add(parameter);
return parameter;
}
mycommand.CommandText = "INSERT INTO images (image_name, tags, relevance) VALUES (@image_name, @tags, @relevance)";
AddParameter(mycommand, "@image_name", DbType.String, "Example Image åäö.jpg");
AddParameter(mycommand, "@tags", DbType.String, "Test");
AddParameter(mycommand, "@relevance", DbType.Int32, 3);
mycommand.ExecuteNonQuery();
不要使用DbType.AnsiString,否則您將遇到與現在相同的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.