master
wanggang 4 years ago
parent ab0ca7af82
commit bc0d9bd2b6

@ -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<Dictionary<string, object>>();
try
{
var list = new List<object>();
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<string, object>();
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<object>();
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<object>();
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)

@ -10,7 +10,7 @@
<DebugType>full</DebugType>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<SatelliteResourceLanguages>zh-Hans</SatelliteResourceLanguages>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

@ -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';"
}))));
</script>
<script>
var width = 358;

Loading…
Cancel
Save