Exemplo n.º 1
0
        // 获取 DatetTime 类型的 SQL 片断(包括DateTime和TimeStamp)
        protected override string GetSqlValueByDateTime(object value, object dbType, int?precision)
        {
            // 默认精度为0
            string          format     = "yyyy-MM-dd HH:mm:ss";
            MySqlDbTypeInfo dbTypeInfo = MySqlDbTypeInfo.Create(dbType);

            if (dbTypeInfo != null && dbTypeInfo.IsDate)
            {
                format = "yyyy-MM-dd";
            }
            else if (dbTypeInfo != null && (dbTypeInfo.IsDateTime || dbTypeInfo.IsDateTime2))
            {
                string pad = string.Empty;
                if (precision != null && precision.Value > 0)
                {
                    pad = "f".PadLeft(precision.Value > 6 ? 6 : precision.Value, 'f');
                }
                if (!string.IsNullOrEmpty(pad))
                {
                    format = string.Format("yyyy-MM-dd HH:mm:ss.{0}", pad);
                }
            }

            string result = this.EscapeQuote(((DateTime)value).ToString(format), false, false);

            return(result);
        }
Exemplo n.º 2
0
        // 增加一个参数
        protected override IDbDataParameter AddParameter(object value, object dbType, int?size = null, int?precision = null, int?scale = null, ParameterDirection?direction = null)
        {
            MySqlParameter parameter = (MySqlParameter)base.AddParameter(value, dbType, size, precision, scale, direction);

            // 补充 DbType
            MySqlDbTypeInfo dbTypeInfo = MySqlDbTypeInfo.Create(dbType);

            if (dbTypeInfo != null && dbTypeInfo.DbType != null)
            {
                parameter.DbType = dbTypeInfo.DbType.Value;
            }
            else if (dbTypeInfo != null && dbTypeInfo.SqlDbType != null)
            {
                parameter.MySqlDbType = dbTypeInfo.SqlDbType.Value;
            }

            if (size != null && (size.Value > 0 || size.Value == -1))
            {
                parameter.Size = size.Value;
            }
            if (precision != null && precision.Value > 0)
            {
                parameter.Precision = (byte)precision.Value;
            }
            if (scale != null && scale.Value > 0)
            {
                parameter.Scale = (byte)scale.Value;
            }

            return(parameter);
        }