Ejemplo n.º 1
0
        /// <summary>
        /// 创建一个SQL参数实例。SQL参数的类型由参数的值决定。
        /// </summary>
        /// <param name="client"><see cref="IDbClient"/>的实例。</param>
        /// <param name="name">参数的名称。</param>
        /// <param name="value">参数的值。</param>
        /// <returns><see cref="DbParameter"/>的实例。</returns>
        public static DbParameter CreateParameter(this IDbClient client, string name, object value)
        {
            var p = client.CreateParameter();

            p.ParameterName = name;

            if (value == null)
            {
                p.Value = DBNull.Value;
                p.DbType = DbType.AnsiString;
                p.Size = 1;
            }
            else
            {
                var dbType = DbTypeConvert.LookupDbType(value.GetType());

                if (dbType == DbTypeConvert.NotSupporteDbType)
                {
                    throw new NotSupportedException(string.Format(
                        "The type {0} can not be converted to a DbType.", value.GetType()));
                }

                p.DbType = dbType;
                p.Value = value;

                var stringValue = value as string;
                if (stringValue != null && stringValue.Length <= DbTypeConvert.DefaultStringSizeForDbParameter)
                {
                    p.Size = DbTypeConvert.DefaultStringSizeForDbParameter;
                }
            }

            return p;
        }
Ejemplo n.º 2
0
            public void AddDbParameterInfo(string paramName, MemberInfo memberInfo)
            {
                var isPropertyInfo = memberInfo is PropertyInfo;
                var memberType     = isPropertyInfo
                    ? ((PropertyInfo)memberInfo).PropertyType
                    : ((FieldInfo)memberInfo).FieldType;
                var dbType = DbTypeConvert.LookupDbType(memberType);

                if (dbType == DbTypeConvert.NotSupporteDbType)
                {
                    throw new NotSupportedException(string.Format(
                                                        "The type {0} can not be converted to a DbType.", memberType));
                }

                var info = new DbParameterInfo();

                info.Name        = paramName;
                info.DbType      = dbType;
                info.IsString    = memberType == typeof(string);
                info.ValueGetter = isPropertyInfo
                    ? PropertyAccessorGenerator.CreateGetter((PropertyInfo)memberInfo)
                    : FieldAccessorGenerator.CreateGetter((FieldInfo)memberInfo);

                _parameterInfos.Add(info);
            }
Ejemplo n.º 3
0
        private static DbType LookupDbType(Type type)
        {
            var dbType = DbTypeConvert.LookupDbType(type);

            if (dbType == DbTypeConvert.NotSupporteDbType)
            {
                throw new NotSupportedException($"The type {type} can not be converted to DbType.");
            }

            return(dbType);
        }
Ejemplo n.º 4
0
        private static IEnumerable <DbParameter> GenerateParameters(IDbClient client, object[] param)
        {
            for (int i = 0; i < param.Length; i++)
            {
                var value = param[i];
                var p     = value as DbParameter;

                if (p != null)
                {
                    yield return(p);
                }

                p = client.CreateParameter();
                p.ParameterName = i.ToString(CultureInfo.InvariantCulture);

                if (value == null)
                {
                    p.Value  = DBNull.Value;
                    p.DbType = DbType.AnsiString;
                    p.Size   = 1;
                }
                else
                {
                    var dbType = DbTypeConvert.LookupDbType(value.GetType());

                    if (dbType == DbTypeConvert.NotSupporteDbType)
                    {
                        throw new NotSupportedException(string.Format(
                                                            "The type {0} can not be converted to a DbType.", value.GetType()));
                    }

                    p.Value  = value;
                    p.DbType = dbType;

                    var stringValue = value as string;
                    if (stringValue != null && stringValue.Length <= DbTypeConvert.DefaultStringSizeForDbParameter)
                    {
                        p.Size = DbTypeConvert.DefaultStringSizeForDbParameter;
                    }
                }

                yield return(p);
            }
        }