From bc0d9bd2b6474c0d2ef1d5efb94aa17ded9e7611 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 14 Dec 2021 15:58:49 +0800 Subject: [PATCH] update --- src/PhotoCollector/MainForm.cs | 102 +++++++++++++++++++++++ src/PhotoCollector/PhotoCollector.csproj | 2 +- src/PhotoCollector/wwwroot/index.html | 13 +++ 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/PhotoCollector/MainForm.cs b/src/PhotoCollector/MainForm.cs index 32080c5..9fbb8c5 100644 --- a/src/PhotoCollector/MainForm.cs +++ b/src/PhotoCollector/MainForm.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; using PhotoCollector.Data; @@ -258,6 +259,107 @@ namespace PhotoCollector message }, jsonSerializerOptions); }); + WebView2Interop.FuncList.Add("executeQuery", o => + { + var code = 0; + var message = "操作成功"; + var value = new List>(); + try + { + var list = new List(); + using var db = new AppDbContext(); + using var cmd = db.Database.GetDbConnection().CreateCommand(); + cmd.CommandText = o["sql"]; + db.Database.OpenConnection(); + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + var item = new Dictionary(); + for (int i = 0; i < reader.FieldCount; i++) + { + item.Add(reader.GetName(i), reader.IsDBNull(i) ? null : reader.GetValue(i)); + } + value.Add(item); + } + } + } + catch (Exception ex) + { + Log.Error(ex.ToString()); +#if DEBUG + MessageBox.Show(ex.Message); +#endif + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message, + value = value, + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("executeCommand", o => + { + var code = 0; + var message = "操作成功"; + var value = 0; + try + { + var list = new List(); + using var db = new AppDbContext(); + using var cmd = db.Database.GetDbConnection().CreateCommand(); + cmd.CommandText = o["sql"]; + db.Database.OpenConnection(); + value = cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Log.Error(ex.ToString()); +#if DEBUG + MessageBox.Show(ex.Message); +#endif + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message, + value = value, + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("executeScalar", o => + { + var code = 0; + var message = "操作成功"; + object value = null; + try + { + var list = new List(); + using var db = new AppDbContext(); + using var cmd = db.Database.GetDbConnection().CreateCommand(); + cmd.CommandText = o["sql"]; + db.Database.OpenConnection(); + value = cmd.ExecuteScalar(); + } + catch (Exception ex) + { + Log.Error(ex.ToString()); +#if DEBUG + MessageBox.Show(ex.Message); +#endif + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message, + value = value, + }, jsonSerializerOptions); + }); } private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) diff --git a/src/PhotoCollector/PhotoCollector.csproj b/src/PhotoCollector/PhotoCollector.csproj index b947c8f..e62276a 100644 --- a/src/PhotoCollector/PhotoCollector.csproj +++ b/src/PhotoCollector/PhotoCollector.csproj @@ -10,7 +10,7 @@ full True zh-Hans - 0.1.2 + 0.1.3 diff --git a/src/PhotoCollector/wwwroot/index.html b/src/PhotoCollector/wwwroot/index.html index 7d39a6a..604bb5d 100644 --- a/src/PhotoCollector/wwwroot/index.html +++ b/src/PhotoCollector/wwwroot/index.html @@ -201,6 +201,19 @@ alert("鍒犻櫎閰嶇疆鎴愬姛"); } } + //閫氱敤鏁版嵁搴撴帴鍙 + console.dir(JSON.parse(window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({ + command: "executeQuery", + sql: "select * from configs where key = 'test';" + })))); + console.dir(JSON.parse(window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({ + command: "executeCommand", + sql: "update configs set value = 'testValueUpdated' where key = 'test';" + })))); + console.dir(JSON.parse(window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({ + command: "executeScalar", + sql: "select value from configs where key = 'test';" + }))));