Skip to content

数据库交互

在服务端命令中,可以通过 dataContext.DataAccess 属性对数据库进行增删改查

本例中使用的示例数据库如下
1670169030816-0fb5c11a-2376-4c7c-90b1-bd370f4c3169.png

获取数据示例代码

参数为OData字符串

csharp
using GrapeCity.Forguncy.Commands;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var data = dataContext.DataAccess.GetTableData("表1?$select=ID,字段1,小数");

            return new ExecuteResult() { Message = JsonConvert.SerializeObject(data) };
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

效果如下

1670168117484-32887991-bd4e-4b3a-9225-74f92aa0d297.png

新增数据示例代码

csharp
using GrapeCity.Forguncy.Commands;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            dataContext.DataAccess.AddTableData("表1", new Dictionary<string, object>
            {
                {"字段1", "xxx" },
                {"小数", "1.5" }
            });

            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

执行结果
1670168332436-41b6391f-6b1d-47c8-897d-e357a519e5b1.png

删除数据示例代码

csharp
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.ServerApi;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            dataContext.DataAccess.DeleteTableData("表1", new ColumnValuePair()
            {
                ColumnName = "ID",
                Value = 2
            });

            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

执行结果
1670168580428-50661d0b-6336-40b6-a08b-5e52fd92507b.png

更新数据示例代码

csharp
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.ServerApi;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            dataContext.DataAccess.UpdateTableData("表1", new ColumnValuePair()
            {
                ColumnName = "ID",
                Value = 2
            },
            new Dictionary<string, object>()
            {
                {"字段1", "xxx" },
                {"小数", "1.5" }
            });

            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

执行结果

1670168879572-96355d77-4def-4d9b-b006-c5404c75a693.png

更新: 2022-12-04 23:50:33
原文: https://www.yuque.com/robert-bh51n/ea8l6c/ncduv1srmpmd9w6x