Esempio n. 1
0
        /// <summary>
        /// 生成参数。
        /// </summary>
        /// <param name="paramName">参数名。</param>
        /// <param name="direction">参数方向。</param>
        /// <param name="dataType">数据类型。</param>
        /// <param name="size">长度。</param>
        /// <param name="paramValue">参数值。</param>
        /// <returns>构造好的参数对象。</returns>
        /// <remarks>
        /// 注意:重写的目的是为参数类型赋值。
        /// 若数据类型为Default,不处理参数类型字段。
        /// </remarks>
        public override IDbDataParameter MakeParam(string paramName, ParameterDirection direction, GSPDbDataType dataType, int size, object paramValue)
        {
            SqlParameter param = (SqlParameter)base.MakeParam(paramName, direction, dataType, size, paramValue);

            //兼容nvarchar(max)类型字段,绑定变量4001-8000范围时报The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect" exception when using NVarchar parameters with Sqlclient
            //https://support.microsoft.com/en-us/kb/970519
            //Status: Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. This is scheduled to be addressed in the next major release of .NET Framework
            //Resolution:To work around this issue, use one of the following options:
            //· Set Sqlparamter.size property to -1 to ensure that you are getting the entire data from the backend without truncation.
            //· When working with String DbTypes whose sizes are greater than 4000, explicitly map them to another SqlDBType like NText instead of using NVarchar(which also is the default SqlDBType for strings).
            //· Use a value that is not between 4001 and 8000 for Sqlparameter.size.
            if (size > 0)
            {
                param.Size = size;
            }
            else
            {
                param.Size = -1;
            }

            if (dataType != GSPDbDataType.Default)
            {
                param.SqlDbType = (SqlDbType)DBTypeManager.GetDBType(this.DbType, dataType);
            }
            return(param);
        }
Esempio n. 2
0
        /// <summary>
        /// 生成参数。
        /// </summary>
        /// <param name="paramName">参数名。</param>
        /// <param name="direction">参数方向。</param>
        /// <param name="dataType">数据类型。</param>
        /// <param name="size">长度。</param>
        /// <param name="paramValue">参数值。</param>
        /// <returns>构造好的参数对象。</returns>
        /// <remarks>
        /// 注意:重写的目的是为参数类型赋值。
        /// 若数据类型为Default,不处理参数类型字段。
        /// </remarks>
        public override IDbDataParameter MakeParam(string paramName, ParameterDirection direction, GSPDbDataType dataType, int size, object paramValue)
        {
            //NpgsqlParameter param = (NpgsqlParameter)base.MakeParam(paramName, direction, dataType, size, paramValue);

            NpgsqlParameter param = this.CreateDBDataParameter() as NpgsqlParameter;

            param.ParameterName = paramName;

            if (size > 0)
            {
                param.Size = size;
            }
            if (dataType != GSPDbDataType.Default)
            {
                param.NpgsqlDbType = (NpgsqlDbType)DBTypeManager.GetDBType(this.DbType, dataType);
            }
            else
            {
                //如果都用bit存储boolean值,则放开;如果用boolean类型存储boolean,则放开
                //if (paramValue != null && paramValue != DBNull.Value && paramValue is bool)
                //{
                //    //反射微软驱动:Boolean是对应byte
                //    //new MetaType(DbType.Boolean, OracleType.Byte, OCI.DATATYPE.UNSIGNEDINT, "UNSIGNED INTEGER", typeof(byte), typeof(byte), 1, 1, false)
                //    param.NpgsqlDbType = NpgsqlDbType.Bit;
                //}
                //加这一句是对枚举类型的报错
                if (paramValue != null && paramValue != DBNull.Value && paramValue.GetType().IsEnum)
                {
                    param.NpgsqlDbType = (NpgsqlDbType)DBTypeManager.GetDBType(this.DbType, GSPDbDataType.Int);
                }
            }
            //参数方向
            param.Direction = direction;

            //设置参数值,对于DateTime类型,特殊处理下
            if (direction != ParameterDirection.Output || (paramValue != null && paramValue != DBNull.Value))
            {
                //需要特殊处理的
                //日期类型的
                if (paramValue != null && paramValue != DBNull.Value && dataType == GSPDbDataType.DateTime)
                {
                    DateTime date = Convert.ToDateTime(param.Value);
                    param.Value = date;
                }
                else
                {
                    param.Value = paramValue;
                }
            }
            return(param);
        }