我有这段代码从 sql lite 数据库中读取数据并在 wpf 应用程序中以手风琴形式显示它
int la = 1;
if (langid == "Arabic")
{
la = 2;
}
DbCommand cmdSelect = conn.CreateCommand();
// cmdSelect.CommandText = "SELECT id from GenericContentCategories where UniqueName=@UniqueName and languageId=@langid";
List<GenericContentView> res = new List<GenericContentView>();
cmdSelect.CommandText = "SELECT title,content,MainImage,startDate from News where languageId=@langid";
DatabaseManager.AddParameter(cmdSelect, "@UniqueName", UniqeName);
DatabaseManager.AddParameter(cmdSelect, "@langid", la);
var reader = cmdSelect.ExecuteReader();
while (reader.Read())
{
GenericContentView fetchedItem = new GenericContentView();
fetchedItem.Title = reader["title"] as string;
fetchedItem.Content = reader["content"] as string;
fetchedItem.MainImage = reader["MainImage"] as string;
fetchedItem.startDate = (reader["startDate"] as DateTime?).Value;
res.Add(fetchedItem);
}
return res;
}
项目内容在垃圾数据中返回阿拉伯语
这是将这些数据从 sql server 写入 sqllite 的代码
public void CacheEntries(DateTime lastCacheTime) { 列表猫 = 新列表 ();
cats.AddRange(GetGenericCategories(new DateTime(), null, 1));
cats.AddRange(GetGenericCategories(new DateTime(), null, 2));
List<GenericContentView> Contents = new List<GenericContentView>();
Contents.AddRange(GetGenericItems(new DateTime(), null, 1));
Contents.AddRange(GetGenericItems(new DateTime(), null, 2));
DatabaseManager.Instance.OpenConnection();
DbConnection conn = DatabaseManager.Instance.GetConnection();
DbTransaction transaction = conn.BeginTransaction();
try
{
DbCommand cmdGCCreate = conn.CreateCommand();
cmdGCCreate.CommandText = "CREATE TABLE IF NOT EXISTS GenericContent (id INTEGER PRIMARY KEY, title VARCHAR(255),UniqueName VARCHAR(255),content VARCAHR(5000), startDate DATETIME, relatedEntities VARCHAR(5000), MainImage VARCHAR(5000),Description VARCHAR(5000), categoryId VARCHAR(5000))";
cmdGCCreate.ExecuteNonQuery();
DbCommand cmdInsertGC = conn.CreateCommand();
DbCommand cmdUpdateGC = conn.CreateCommand();
cmdInsertGC.CommandText = "INSERT OR IGNORE INTO GenericContent (id) VALUES (@id)";
cmdUpdateGC.CommandText = "Update GenericContent SET title = @title,UniqueName=@UniqueName, content = @content, startDate = @startDate, relatedEntities = @relatedEntities,Description=@Description, categoryId = @categoryId,MainImage=@MainImage WHERE id = @id";
DbCommand cmdCatCreate = conn.CreateCommand();
cmdCatCreate.CommandText = "CREATE TABLE IF NOT EXISTS GenericContentCategories (id INTEGER PRIMARY KEY, parentId Integer, name VARCAHR(5000), languageId Integer,UniqueName VARCHAR(255))";
cmdCatCreate.ExecuteNonQuery();
DbCommand cmdInsertCat = conn.CreateCommand();
DbCommand cmdUpdateCat = conn.CreateCommand();
cmdInsertCat.CommandText = "INSERT OR IGNORE INTO GenericContentCategories (id) VALUES (@id)";
cmdUpdateCat.CommandText = "Update GenericContentCategories SET parentId = @parentId, name = @name, languageId = @languageId,UniqueName=@UniqueName WHERE id = @id";
AddCategories(cats.ToArray(), null,cmdInsertCat, cmdUpdateCat);
AddGenericContent(Contents,cmdInsertGC, cmdUpdateGC);
transaction.Commit();
}
catch (Exception e)
{
LOG.ERROR(e);
transaction.Rollback();
}
finally
{
DatabaseManager.Instance.CloseConnection();
}
}