|||
using System;
using System.Collections.Generic;
using System.Text;
using FWFramework.Service.Common;
using FWFramework.Common;
using System.Data;
using FWFramework.Data;
using FWFramework.TableReader;
namespace MyDataService
{
/// <summary>
/// 数据服务,必须继承ServiceBase 或IService
/// </summary>
public class MyDataService1: ServiceBase
{
//定义一个数据服务。数据服务可访问所有平台连接的数据库。
//可进行数据库读取,写入操作。
DataService service = new DataService();
/// <summary>
/// 重写ServiceName属性,定义名称。
/// </summary>
public override string ServiceName
{
get { return "MY_SERVICE1"; }
}
/// <summary>
/// 可通过重写此方法,绕过登录验证,此方法一般不需要重写。
/// 以下示例是绕过验证的写法。
/// </summary>
/// <param name="cmd"></param>
/// <param name="errMessage"></param>
/// <returns></returns>
protected override int CheckCommand(FWCommand cmd, out string errMessage)
{
//
//可注销下面一行绕过验证,即没有登录也可以获取数据,请谨慎使用。
//return base.CheckCommand(cmd, out errMessage);
errMessage = string.Empty;
return 0;
}
protected override FWReturnData RunCommand(FWCommand cmd, out bool done)
{
done = true;
FWReturnData rd = new FWReturnData();
switch (cmd.Command)
{
case 1000:
DataTable dt = getMyData();
rd.Value = dt;
return rd;
case 1001:
DataTable dt1 = getMyData(cmd.CommandData as Dictionary<string, object>);
rd.Value = dt1;
return rd;
}
//如果没有处理到,一定要将done置为false,以便后续程序继续处理。
done = false;
return null;
}
/// <summary>
/// 不带参数
/// </summary>
/// <returns></returns>
private DataTable getMyData()
{
//这个是示例程序,一般需要编写服务的情况是在业务表SQL无法完成的情况下才需要通过服务方式进行。
string sql = "SELECT * FROM SA_DEPT_DICT";
DataTable dt = service.CustomSelect("default", sql);
return dt;
}
/// <summary>
/// 带参数,参数是通过Dictionary<string, object>传输的。
/// </summary>
/// <param name="deptType"></param>
/// <param name="deptName"></param>
/// <returns></returns>
private DataTable getMyData(Dictionary<string, object> p)
{
if (!p.ContainsKey("DEPT_TYPE"))
{
throw new Exception("参数不足");
}
if (!p.ContainsKey("DEPT_NAME"))
{
throw new Exception("参数不足");
}
int deptType = Convert.ToInt32(p["DEPT_TYPE"]);
string deptName = p["DEPT_NAME"].ToString();
string sql = string.Format("SELECT * FROM SA_DEPT_DICT where DEPT_TYPE={0} and DEPT_NAME ='{1}'", deptType, deptName);
DataTable dt = service.CustomSelect("default", sql);
return dt;
}
}
}
|
//定义服务
DataService dservice = new DataService();
//定义命令
FWCommand cmd = new FWCommand();
cmd.ServiceName = "MY_SERVICE1";
cmd.Command = 1001;
//如果有参数,则将参数和值一起传递到服务端。
Dictionary<string, object> param = new Dictionary<string, object>();
param.Add("DEPT_TYPE", 1);
param.Add("DEPT_NAME", "主任");
cmd.CommandData = param;
FWReturnData rd = dservice.GetExtendData(cmd, true);
DataTable dt1 = rd.Value as DataTable;
|
Powered by Discuz! X3.2
© 2001-2013 Comsenz Inc.