客户端调用WCF的时候报上面的错误,WCF只能序列化基础的数据类型,不能直接序列化SqlParameter类型,需要使用自定义类,然后在WCF服务端转换的方式解决:

自定义类代码如下:

 using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace CommonLib.CustomClass
{
/// <summary>
/// 方法标记为DataContract约束,属性标记为DataMember
/// </summary>
[Serializable]
[DataContract]
public class SetSqlParameter
{
#region 属性 /// <summary>
/// 参数名称
/// </summary>
[DataMember]
private string paraName = "";
public string ParaName
{
get { return this.paraName; }
set { this.paraName = value; } } /// <summary>
/// 参数长度
/// </summary>
[DataMember]
private int paraLength = ;
public int ParaLength
{ get { return this.paraLength; }
set { this.paraLength = value; }
} /// <summary>
/// 参数值
/// </summary>
[DataMember]
private object paraValue = null;
public object ParaValue
{
get { return this.paraValue; }
set { this.paraValue = value; }
} /// <summary>
/// 参数类型
/// </summary>
[DataMember]
private SqlDbType paraDbType = SqlDbType.NVarChar;
public SqlDbType ParaDbType
{
get { return this.paraDbType; } set { this.paraDbType = value; }
} #endregion /// <summary>
/// 构造函数
/// </summary>
/// <param name="sPara"></param>
public SetSqlParameter(SqlParameter sPara)
{
this.paraName = sPara.ParameterName;
this.paraLength = sPara.Size;
this.paraValue = sPara.Value;
this.paraDbType = sPara.SqlDbType;
} /// <summary>
/// 转换成SqlParameter类型
/// </summary>
/// <returns></returns>
public SqlParameter ConvertToSqlParameter()
{
SqlParameter parameter = new SqlParameter(this.paraName, this.paraDbType, this.paraLength);
parameter.Value = this.paraValue;
return parameter;
}
}
}

WCF服务端代码如下:

接口代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CommonLib.CustomClass; namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IMyService”。
[ServiceContract]
public interface IMyService
{
[OperationContract]
DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters);
}
}

接口实现类代码:

 using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Configuration;
using CommonLib.CustomClass; namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“MyService”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 MyService.svc 或 MyService.svc.cs,然后开始调试。
public class MyService : IMyService
{ public DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters)
{
DataTable dtReturn = new DataTable();
dtReturn.TableName = "ExecuteQuery";
string strCon = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strCon))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
conn.Open();
if (parameters != null)
{
SqlParameter[] para = new SqlParameter[parameters.Length];
for (int i = ; i < parameters.Length; i++)
{
//把SetSqlParameter类型的数组转换成SqlParameter类型的数组
para[i] = parameters[i].ConvertToSqlParameter();
}
cmd.Parameters.AddRange(para);
} SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dtReturn);
}
return dtReturn;
}
}
}

客户端调用WCF代码:

 using CommonLib.CustomClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace winClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btn_GetData_Click(object sender, EventArgs e)
{ string strSQL = " SELECT * FROM BaseSetMainInfo WHERE TypeCode=@TypeCode "; //定义SqlParameter
SqlParameter para = new SqlParameter("@TypeCode", SqlDbType.Int);
para.Value = ; //定义SetSqlParameter类型的数组
SetSqlParameter[] paras = new SetSqlParameter[] {
new SetSqlParameter(para)
}; //实例化WCF服务
ServiceReference.MyServiceClient client=new ServiceReference.MyServiceClient();
//调用WCF服务提供的方法
DataTable dt = client.ExeceteQuery(strSQL, paras);
this.dataGridView1.DataSource = dt; }
}
}

这样就可以解决WCF不能直接序列化SqlParameter类型的问题了。

代码下载地址:https://files.cnblogs.com/files/dotnet261010/WcfSqlParameterDemo.rar

最新文章

  1. 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)
  2. Centos7安装Mono(以4.6.0)为例
  3. JavaScript 闭包系列二(匿名函数及函数的闭包)
  4. 正确的选择log级别
  5. [Architect] ABP(现代ASP.NET样板开发框架) 翻译
  6. POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”
  7. nodejs-fs使用
  8. web标准(复习)--2 列布局
  9. Xcode7 使用NSURLSession发送HTTP请求报错
  10. &lt;meta&gt;标签的作用
  11. ecshop订单状态对应值详解
  12. 【UML 建模】状态图介绍
  13. react安装 项目构建
  14. Python基础-数据类型-转摘
  15. JavaWeb基础之Servlet简单实现用户登陆
  16. 关于 python中的转义字符
  17. Code once, debug everywhere.
  18. JS报错修改日记(1):Uncaught ReferenceError: showQRcode is not defined
  19. C#、AE开发入门之打开CAD文件并显示
  20. 基于开源SuperSocket实现客户端和服务端通信项目实战

热门文章

  1. /etc/vsftpd.conf配置(ftp上传)
  2. 在低带宽或不可靠的网络环境中安装 Visual Studio 2017
  3. [Grunt] Watch &amp;&amp; grunt-contrib-watch
  4. 清除tomcat的缓存
  5. WCF学习笔记之并发与限流
  6. 金蝶K3,域环境中,无本地用户管理员权限的域用户如何设置注册表权限?
  7. Patterns-Proxy
  8. Android中源码Launcher主屏幕程序排列详解【安卓Launcher进化一】
  9. EMQ ---websocket
  10. C#基础—不安全代码(unsafe code)