[UWP] [C#]在某些条件下将数据从服务器(json)插入数据库 [英] [UWP][C#]Insert data from the server (json) into the database with certain conditions
问题描述
我有一个应用程序可以检查数据服务器(json)和数据库。我想当json上的id与数据库中的id不同时,它会将
所有具有不等id的数据插入到数据库中。
代码:
var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path," ebookstore.db" );
this.DataContextChanged + =(s,e1)=> {UpdateViewModel = DataContext as ViewModels.UpdateViewModel; };
string idDb = @" SELECT id FROM books其中parent_folder_id = 2,标题如'%guru%'" ;;
var IDdb = objConnUpdate.Prepare(idDb);
IDdb.Step();
iddb = IDdb [0] .ToString();
IDDB = Convert.ToInt32(iddb.ToString());
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
{
try
{
Downloading.IsOpen = true;
string urlPath1 =" https://.../fetch/k13G" ;;
var httpClient1 = new HttpClient(new HttpClientHandler());
httpClient1.DefaultRequestHeaders.TryAddWithoutValidation(" KIAT-API-KEY"," ... *");
var values1 = new List< KeyValuePair< string,string>>
{
new KeyValuePair< string,string>(" halaman"," 1"),
new KeyValuePair< string,string>(" limit"," 20") ,
};
var response1 = await httpClient1.PostAsync(urlPath1,new FormUrlEncodedContent(values1));
response1.EnsureSuccessStatusCode();
string jsonText1 = await response1.Content.ReadAsStringAsync();
JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
JsonArray jsonData1 = jsonObject1 [" data"]。GetArray();
foreach(JsonVal中的jsonValue groupValue)
{
JsonObject groupObject = groupValue.GetObject();
string ID = groupObject [" id"]。GetString();
BukuUpdate file1 = new BukuUpdate();
file1.ID = ID;
int intID = Convert.ToInt32(file1.ID);
if(intID!= IDDB)
{
string jumlahidDb = @" SELECT COUNT(id)FROM books其中parent_folder_id = 2且id> " + IDDB +"和标题如'%guru%'" ;;
var jumlahIdDB = objConnUpdate.Prepare(jumlahidDb);
jumlahIdDB.Step();
if(jumlahiddb< jumlahbuku)
{;
if(nama ==" Kelas_01_SD_")
{
DownloadBukuK2013G(url);
string K2013GUpdate = @" INSERT INTO books(id,title,folder_id,identifier,parent_folder_id)SELECT" + intID +",'" + namaFile +" .pdf',34,'" + namaFile +
" .pdf',2 WHERE not exists(选择id和title AND folder_id AND identifier AND parent_folder_id FROM books WHERE id =" + intID +" and title ='" + namaFile +
" .pdf'AND folder_id = 34 and identifier ='" + namaFile +" .pdf'和parent_folder_id = 2)" ;;
var K2013GQuery = objConnUpdate.Prepare(K2013GUpdate);
K2013GQuery.Step();
}
BukuUpdate.cs:
公共类BukuUpdate
{
public string ID {get;组; }
}
我没有成功实现它。如何处理?
嗨Priscillia,
很难找到问题在哪里。但是你已经在C#代码中获得了ID。我建议您从数据库中获取所有
的数据。然后使用C#代码判断ID属性是否相同。之后,将所有具有不同id的数据集成并保存到数据库中。
祝你好运,
Roy < /跨度>
I have an app that checks the data on the server (json) and the database. I want when the id on json is not the same as the id on the database, it will insert all data with unequal id into the database.
Code:
var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ebookstore.db"); this.DataContextChanged += (s, e1) => { UpdateViewModel = DataContext as ViewModels.UpdateViewModel; }; string idDb = @"SELECT id FROM books where parent_folder_id = 2 and title like '%guru%'"; var IDdb = objConnUpdate.Prepare(idDb); IDdb.Step(); iddb = IDdb[0].ToString(); IDDB = Convert.ToInt32(iddb.ToString()); ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile(); { try { Downloading.IsOpen = true; string urlPath1 = "https://.../fetch/k13G"; var httpClient1 = new HttpClient(new HttpClientHandler()); httpClient1.DefaultRequestHeaders.TryAddWithoutValidation("KIAT-API-KEY", "...*"); var values1 = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("halaman", "1"), new KeyValuePair<string, string>("limit", "20"), }; var response1 = await httpClient1.PostAsync(urlPath1, new FormUrlEncodedContent(values1)); response1.EnsureSuccessStatusCode(); string jsonText1 = await response1.Content.ReadAsStringAsync(); JsonObject jsonObject1 = JsonObject.Parse(jsonText1); JsonArray jsonData1 = jsonObject1["data"].GetArray(); foreach (JsonValue groupValue in jsonData1) { JsonObject groupObject = groupValue.GetObject(); string ID = groupObject["id"].GetString(); BukuUpdate file1 = new BukuUpdate(); file1.ID = ID; int intID = Convert.ToInt32(file1.ID); if (intID != IDDB) { string jumlahidDb = @"SELECT COUNT(id) FROM books where parent_folder_id = 2 and id > " + IDDB + " and title like '%guru%'"; var jumlahIdDB = objConnUpdate.Prepare(jumlahidDb); jumlahIdDB.Step(); if (jumlahiddb < jumlahbuku) {; if (nama == "Kelas_01_SD_") { DownloadBukuK2013G(url); string K2013GUpdate = @"INSERT INTO books (id,title,folder_id,identifier,parent_folder_id) SELECT " + intID + ",'" + namaFile + ".pdf',34,'" + namaFile + ".pdf',2 WHERE not exists (select id AND title AND folder_id AND identifier AND parent_folder_id FROM books WHERE id=" + intID + " and title='" + namaFile + ".pdf' AND folder_id=34 and identifier='" + namaFile + ".pdf' and parent_folder_id=2)"; var K2013GQuery = objConnUpdate.Prepare(K2013GUpdate); K2013GQuery.Step(); }
BukuUpdate.cs:
public class BukuUpdate { public string ID { get; set; } }
I did not succeed in implementing it. How to handle it?
Hi Priscillia,
Hard to find out where the problem is. But as you have already get the ID in the C# code. I suggest that you could get all the data from the Database. Then use C# code to judge whether the ID property is the same. After that, integrate all the data with different id and save them into the Database.
Best regards,
Roy
这篇关于[UWP] [C#]在某些条件下将数据从服务器(json)插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!