WCF SqlParameter序列化问题解决方案

博文 http://www.cnblogs.com/pan11jing/archive/2011/08/19/2051827.html 通过自定义类,再在WCF端转换的方式解决问题,之后出现了一个很小的不便

先给出原博文代码:

首先是添加在WCF端的类:

    [Serializable]
    [DataContract]
    public class SerSqlParameter
    {
        public SerSqlParameter(SqlParameter sPara)
        {
            this.paraName = sPara.ParameterName;
            this.paraLen = sPara.Size;
            this.paraVal = sPara.Value;
            this.sqlDbType = sPara.SqlDbType;
        }

        public SqlParameter ToSqlParameter()
        {
            SqlParameter para = new SqlParameter(this.paraName, this.sqlDbType, this.paraLen);
            para.Value = this.paraVal;
            return para;
        }

        [DataMember]
        public string ParaName
        {
            get { return this.paraName; }
            set { this.paraName = value; }

        }
        private string paraName = "";

        [DataMember]
        public int ParaLen
        {

            get { return this.paraLen; }
            set { this.paraLen = value; }
        }
        private int paraLen = 0;

        [DataMember]
        public object ParaVal
        {
            get { return this.paraVal; }
            set { this.paraVal = value; }
        }
        private object paraVal = null;

        [DataMember]
        public SqlDbType SqlDbType
        {
            get { return this.sqlDbType; }

            set { this.sqlDbType = value; }
        }
        private SqlDbType sqlDbType = SqlDbType.NVarChar;
    }

然后在WCF方法中,使用:

public List<QueryResult_Marketing_DealerQuantity> ExecuteQueryMarketing_DealerQuantityBySql(string sql, string key, params SerSqlParameter[] sqlParameters)
{
        SqlParameter[] p = new SqlParameter[sqlParameters.Length];
        for (int i = 0; i < sqlParameters.Length; i++)
        {
            p[i] = sqlParameters[i].ToSqlParameter();
        }
        return ExecuteQueryBySql<QueryResult_Marketing_DealerQuantity>(sql, key, p);
}

最后是客户端调用:

List<SerSqlParameter> sqlParameters = new List<SerSqlParameter>();

sqlParameters.Add(new SerSqlParameter(new SqlParameter { ParameterName = "@StartDateTime", SqlDbType = SqlDbType.DateTime, Value = model.StartDateTime }));

var resultList = _wcf.ExecuteQueryMarketing_DealerQuantityBySql(searchSql.ToString(), GlobalVariables.gs_safekey, sqlParameters.ToArray());

问题来了,因为我原来的代码是:

List<SqlParameter> sqlParameters = new List<SqlParameter>();

sqlParameters.Add(new SqlParameter { ParameterName = "@StartDateTime", SqlDbType = SqlDbType.DateTime, Value = model.StartDateTime });

var resultList = _wcf.ExecuteQueryMarketing_DealerQuantityBySql(searchSql.ToString(), GlobalVariables.gs_safekey, sqlParameters.ToArray());

对于每一个添加都需要添加 

new SerSqlParameter()

这个工作量,也不是盖的,于是乎想起了这个方法:

 

/// <summary>
/// 附加追加方法
/// </summary>
/// <param name="sqlParameters"></param>
/// <param name="sqlParameter"></param>
public static void Add(this List<SerSqlParameter> sqlParameters, SqlParameter sqlParameter)
{
        SerSqlParameter ser = new SerSqlParameter(sqlParameter);
        sqlParameters.Add(ser);
}

这样之后,对于原代码,只需要改个类名就OK了!

posted @ 2016-04-29 11:06  程序员丁  阅读(794)  评论(0编辑  收藏  举报