我在项目中使用了WCF、LINQ等技术,具体脚本如下:首先是定义服务Service方法:
public DataTable GetAllDeptInfo()
{
using (GxkcERPContext gxkcERPContext = new GxkcERPContext())
{
//IEnumerable<basDepartment> dept = from d in gxkcERPContext.basDepartments
// select d;
var dept = from d in gxkcERPContext.basDepartments
join d1 in gxkcERPContext.basDepartments on d.FParentID equals d1.DepartmentID into deptTable
from d1 in deptTable.DefaultIfEmpty()
select new
{
d.DepartmentID,
单位编码 = d.DepartmentCode,
单位名称 = d.DepartmentName,
全称 = d.DepartmentFullName,
d.FParentID,
所属单位 = d1.DepartmentName,
d.FCancel
}; return dept.ToADOTable(rec => new object[] { dept });
} }
然后是服务接口方法:
//取全部单位记录:
[OperationContract]
//basDepartment[] GetAllDeptInfo();
DataTable GetAllDeptInfo();
最后是客户端的执行方法:
private void loadallinfo()
{
using (BasicServiceClient basDepartment = new BasicServiceClient())
{
List<basDepartment> listdept = new List<basDepartment>(); //basDepartment[] dept = basDepartment.GetAllDeptInfo();//这种类型正常 DataTable dt = basDepartment.GetAllDeptInfo();
//listdept.AddRange(dept);//用这两句正错
//dgv_dept.DataSource = listdept; dgv_dept.DataSource = dt;
}
}
当执行客户端时出现以下的错误提示:“An error occurred while receiving the HTTP response to http://localhost:8080/GxkcERP.BasicService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.”
这里提示是通讯协议不对,我把HTTP换成TC/IP也不对。
注意:在定义服务方法时我把返回方法设计成DataTable类型就不行,如果采用数组类型就没问题,如下面的脚本就正常,(basDepartment是一个Table)。
public basDepartment[] GetAllDeptInfo()
{
using(GxkcERPContext gxkcERPContext = new GxkcERPContext())
{
basDepartment[] dept = (from d in gxkcERPContext.basDepartments
select d).ToArray(); return dept;
} }
但如果用basDepartment[]定义最前面的服务方法时系统会报错,因为方法输出的结构与basDepartment的结构不一样了。请老大帮忙提供一个解决办法,小的不胜感激!
public DataTable GetAllDeptInfo()
{
using (GxkcERPContext gxkcERPContext = new GxkcERPContext())
{
//IEnumerable<basDepartment> dept = from d in gxkcERPContext.basDepartments
// select d;
var dept = from d in gxkcERPContext.basDepartments
join d1 in gxkcERPContext.basDepartments on d.FParentID equals d1.DepartmentID into deptTable
from d1 in deptTable.DefaultIfEmpty()
select new
{
d.DepartmentID,
单位编码 = d.DepartmentCode,
单位名称 = d.DepartmentName,
全称 = d.DepartmentFullName,
d.FParentID,
所属单位 = d1.DepartmentName,
d.FCancel
}; return dept.ToADOTable(rec => new object[] { dept });
} }
然后是服务接口方法:
//取全部单位记录:
[OperationContract]
//basDepartment[] GetAllDeptInfo();
DataTable GetAllDeptInfo();
最后是客户端的执行方法:
private void loadallinfo()
{
using (BasicServiceClient basDepartment = new BasicServiceClient())
{
List<basDepartment> listdept = new List<basDepartment>(); //basDepartment[] dept = basDepartment.GetAllDeptInfo();//这种类型正常 DataTable dt = basDepartment.GetAllDeptInfo();
//listdept.AddRange(dept);//用这两句正错
//dgv_dept.DataSource = listdept; dgv_dept.DataSource = dt;
}
}
当执行客户端时出现以下的错误提示:“An error occurred while receiving the HTTP response to http://localhost:8080/GxkcERP.BasicService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.”
这里提示是通讯协议不对,我把HTTP换成TC/IP也不对。
注意:在定义服务方法时我把返回方法设计成DataTable类型就不行,如果采用数组类型就没问题,如下面的脚本就正常,(basDepartment是一个Table)。
public basDepartment[] GetAllDeptInfo()
{
using(GxkcERPContext gxkcERPContext = new GxkcERPContext())
{
basDepartment[] dept = (from d in gxkcERPContext.basDepartments
select d).ToArray(); return dept;
} }
但如果用basDepartment[]定义最前面的服务方法时系统会报错,因为方法输出的结构与basDepartment的结构不一样了。请老大帮忙提供一个解决办法,小的不胜感激!
2.要返回DataTable的话,为什么不返回强类型的DataTable?
3.试过返回DataSet么?
你可能是xml序列化吧