[]
        
(Showing Draft Content)

数据库交互

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

本例中使用的示例数据库如下:



获取数据示例代码

参数为OData字符串:

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;
        }
    }
}

效果如下:



新增数据示例代码

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;
        }
    }
}

执行结果:



删除数据示例代码

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;
        }
    }
}

执行结果:



更新数据示例代码

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;
        }
    }
}

执行结果: