Пример #1
0
        public string GetBillNo(string branchCode, int billType)
        {
            BillSetting billSetting = GetBillSetting(branchCode, billType);

            if (null == billSetting)
            {
                throw new ArgumentNullException(nameof(billSetting));
            }
            string      Num          = "";
            BILLFORMAT2 SerialFormat = SmartCrudHelper.GetEnumItemByValue <BILLFORMAT2>(billSetting.FSerialFormat);

            if (BILLFORMAT2.BIT36 == SerialFormat)
            {
                Num = AnyRadixConvert.ConvertTo(billSetting.FLstNumber, 36);
            }
            else if (BILLFORMAT2.BIT16 == SerialFormat)
            {
                Num = AnyRadixConvert.ConvertTo(billSetting.FLstNumber, 16);
            }
            else
            {
                Num = billSetting.FLstNumber.ToString();
            }
            if (!string.IsNullOrEmpty(billSetting.FFillChar))
            {
                int Fillbits = billSetting.FSerialbit - (billSetting.FLstSerialPreFix.Length + Num.Length);
                if (Fillbits > 0)
                {
                    Num = Num.PadLeft(Fillbits + Num.Length, billSetting.FFillChar[0]);
                }
            }
            return(string.Format("{0}{1}{2}", billSetting.FPrefix, billSetting.FLstSerialPreFix, Num));
        }
Пример #2
0
 private static object GetDataFieldValue(object obj, DbConnType connType)
 {
     if (SmartCrudHelper.IsNullOrDBNull(obj))
     {
         return(null); // DBNull.Value;  //dapper下不可使用 DBNull.Value
     }
     else
     {
         if (connType == DbConnType.ODPNET)
         {
             Type t = obj.GetType();
             if (t == GuidType)
             {
                 return(obj?.ToString().ToUpper());
             }
             else if (t == Bool)
             {
                 return((byte)(((bool)obj) ? 1 : 0));
             }
             else if (t == SmartCrudHelper.SByte)
             {
                 return(Convert.ToByte(obj));
             }
         }
         return(obj);
     }
 }
Пример #3
0
        /// <summary>
        ///     Returns the .net standard conforming DbProviderFactory.
        /// </summary>
        /// <param name="assemblyQualifiedNames">The assembly qualified name of the provider factory.</param>
        /// <returns>The db provider factory.</returns>
        /// <exception cref="ArgumentException">Thrown when <paramref name="assemblyQualifiedNames" /> does not match a type.</exception>
        private static DbProviderFactory GetFactory(string assemblyQualifiedNames)
        {
            Type ft = SmartCrudHelper.GetType(assemblyQualifiedNames);

            if (ft == null)
            {
                throw new ArgumentException($"Could not load the {assemblyQualifiedNames} DbProviderFactory.");
            }
            return((DbProviderFactory)ft.GetField("Instance").GetValue(null));
        }
Пример #4
0
        /// <summary>
        /// 处理连接串,参数支持Diction<string,object>,DbParameter[] , new{},useBrackets=true则是使用这样的参数 #{Para1},否则就是使用 #Para1#
        /// </summary>
        /// <param name="dbConnType"></param>
        /// <param name="oriSql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string TransSQL(this DbConnType dbConnType, string oriSql, object param, bool useBrackets = false)
        {
            string sql         = oriSql;
            int    countofChar = oriSql.Count(c => c == '#');

            if (2 <= countofChar)
            {
                sql = sql.Replace(CURRENT_TIME, GetCurrentTimeFuncName(dbConnType)).Replace(IS_NULL, GetIsNullFuncName(dbConnType));
                if (null != param)
                {
                    IEnumerable <DbParameter> dbpars = param as IEnumerable <DbParameter>;
                    if (null != dbpars)
                    {
                        foreach (var par in dbpars)
                        {
                            string naturePara     = par.ParameterName.TrimParaName();
                            string parNameInstead = "";
                            if (!useBrackets)
                            {
                                parNameInstead = string.Format("#{0}#", naturePara);
                            }
                            else
                            {
                                parNameInstead = "#{" + naturePara + "}";
                            }
                            sql = sql.Replace(parNameInstead, dbConnType.TreatParaName(naturePara));
                        }
                    }
                    else
                    {
                        IDictionary <string, object> dic = SmartCrudHelper.AsDictionary(param);
                        foreach (var par in dic)
                        {
                            string parNameInstead = "";
                            if (!useBrackets)
                            {
                                parNameInstead = string.Format("#{0}#", par.Key);
                            }
                            else
                            {
                                parNameInstead = "#{" + par.Key + "}";
                            }
                            sql = sql.Replace(parNameInstead, dbConnType.TreatParaName(par.Key));
                        }
                    }
                }
            }
            return(sql);
        }
Пример #5
0
        /// <summary>
        /// 普通SELECT语句取多少记录(用替代法替换)
        /// </summary>
        /// <param name="connInfo"></param>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int Count(this DbContext connInfo, string sql, object param)
        {
            bool useRepl = sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase) &&
                           sql.IndexOf("UNION", StringComparison.OrdinalIgnoreCase) < 0;

            if (useRepl)
            {
                if (!sql.StartsWith("SELECT COUNT(*)", StringComparison.OrdinalIgnoreCase))
                {
                    int           indexFROM    = sql.IndexOf("FROM", 0, StringComparison.OrdinalIgnoreCase);
                    int           indexORDERBY = sql.IndexOf("ORDER BY", 0, StringComparison.OrdinalIgnoreCase);
                    StringBuilder sqlbld       = new StringBuilder(512);
                    sqlbld.Append("SELECT COUNT(*) ");
                    if (0 <= indexORDERBY)
                    {
                        sqlbld.Append(sql.Substring(indexFROM, indexORDERBY - indexFROM));
                    }
                    else
                    {
                        sqlbld.Append(sql.Substring(indexFROM));
                    }
                    return(SmartCrudHelper.GetIntValue(connInfo.ExecuteScalar(sqlbld.ToString(), param)));
                }
                return(connInfo.ExecuteScalar <int>(sql, param));
            }
            else
            {
                IDataReader reader = null;
                try
                {
                    reader = connInfo.ExecuteReader(sql, param);
                    int rows = 0;
                    while (reader.Read())
                    {
                        ++rows;
                    }
                    return(rows);
                }
                finally
                {
                    if (null != reader)
                    {
                        reader.Close();
                    }
                }
            }
        }
Пример #6
0
        public int GetInterId(string branchCode, int billType)
        {
            string      sql  = $"SELECT #ISNULL#(MAX(FMAXID),0) FROM {BillTableName} where  FBranchCode=#BranchCode# and FBillType=#BillType#";
            RequestBase pars = new RequestBase();

            pars.Add("BranchCode", branchCode);
            pars.Add("BillType", billType);
            object ret = _Conn.ExecuteScalar(sql, pars);

            if (SmartCrudHelper.IsNullOrDBNull(ret))
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(ret));
            }
        }
Пример #7
0
        public DbFieldTypeAttribute(DbDataType dbType, int size)
        {
            if (dbType == DbDataType.NONE)
            {
                throw new ArgumentException("dbType");
            }
            this.DbType = dbType;
            if (0 > size)
            {
                throw new ArgumentException("size");
            }
            this.Size = size;
            bool isStrType = SmartCrudHelper.IsStringType(dbType);

            if (0 == this.Size)
            {
                throw new ArgumentException("size");
            }
        }
Пример #8
0
        /// <summary>
        /// 获取建立SQL临时表的语句
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="TmpTableName"></param>
        /// <param name="Fields"></param>
        /// <returns></returns>
        private static string GetCreateSQLTmpTableString(
            DataTable dt, string TmpTableName, params string[] Fields)
        {
            StringBuilder strbld = new StringBuilder(1024);

            strbld.Append(GetDeleteSQLTmpTableString(TmpTableName));
            strbld.AppendFormat(@" create table {0} ( ", TmpTableName);
            foreach (string field in Fields)
            {
                DataColumn dc          = dt.Columns[field];
                Type       detdataType = dc.DataType;
                DATATYPE   dataType    = SmartCrudHelper.GetDataType(detdataType);
                strbld.AppendFormat(" {0} ", field);
                if (dataType == DATATYPE.NUMBER)
                {
                    if (SmartCrudHelper.IsInt(detdataType))
                    {
                        strbld.Append(" INT,");
                    }
                    else
                    {
                        strbld.Append(" DECIMAL(12,2),");
                    }
                }
                else if (dataType == DATATYPE.DATE)
                {
                    strbld.Append(" DATETIME,");
                }
                else
                {
                    strbld.AppendFormat(" VARCHAR({0}),",
                                        dc.MaxLength > 0 ? dc.MaxLength : 56);
                }
            }
            string Ret = strbld.ToString().TrimEnd(new char[] { ',' });

            strbld = null;
            Ret   += " )";
            return(Ret);
        }
Пример #9
0
        /// <summary>
        /// 记录中的NULL要转换成DBNULL.VALUE(只限于 DbParameter)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Record"></param>
        /// <param name="item"></param>
        /// <param name="connType"></param>
        /// <returns></returns>
        public static object GetPropertyValue <T>(T Record, KeyValuePair <string, PropertyInfo> item, DbConnType?connType)
        {
            object ret = item.Value.GetValue(Record, null);

            //没办法,ODP.NET不支持 bool,GUID
            if (connType.HasValue && connType == DbConnType.ODPNET)
            {
                Type t = SmartCrudHelper.GetInnerType(item.Value.PropertyType);
                if (t == GuidType)
                {
                    return(ret?.ToString().ToUpper());
                }
                else if (t == Bool)
                {
                    return((byte)(((bool)ret) ? 1 : 0));
                }
                else if (t == SmartCrudHelper.SByte)
                {
                    return(Convert.ToByte(ret));
                }
            }
            return(ret);
        }
Пример #10
0
 public DbContext(DbConnectInfo dbconnect)
     : this(dbconnect.ToDbConnectionString(), SmartCrudHelper.GetEnumByKey <DbConnType>(dbconnect.ConnType.ToUpper()))
 {
 }
Пример #11
0
 public static RequestBase FromKV <T>(Expression <Func <T, object> > expr, object value) where T : class, new()
 {
     return(FromKV(SmartCrudHelper.GetPropertyName <T>(expr), value));
 }
Пример #12
0
 public static SqlFunction CreateDbNowFunction <T>(this DbContext conn, Expression <Func <T, object> > expr) where T : class, new()
 {
     return(conn.CreateDbNowFunction(SmartCrudHelper.GetPropertyNames <T>(expr)?.ToArray()));
 }
Пример #13
0
 public static int Insert <T>(this DbContext connInfo, T record, SqlFunction repl = null, Expression <Func <T, object> > onlyFields = null, string tableName = "")
     where T : new()
 {
     return(InsertPrimitive <T>(connInfo, new[] { record }, repl, SmartCrudHelper.GetPropertyNames <T>(onlyFields), tableName));
 }
Пример #14
0
        /// <summary>
        /// 返回的字段不能为NULL,最好就是 'Y'
        /// </summary>
        /// <param name="connInfo"></param>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool ExistsPrimitive(this DbContext connInfo, string sql, object param = null)
        {
            object result = connInfo.ExecuteScalar(sql, param);

            return(!SmartCrudHelper.IsNullOrDBNull(result));
        }
Пример #15
0
 public static T Select <T>(this DbContext connInfo, object[] pkValues, Expression <Func <T, object> > onlyFields = null, bool isExceptField = false, string tableName = "", bool forUpdate = false)
     where T : new()
 {
     return(SelectPrimitive <T>(connInfo, pkValues, SmartCrudHelper.GetPropertyNames <T>(onlyFields), isExceptField, tableName, forUpdate: forUpdate));
 }
Пример #16
0
 public static object SingleValue <T>(this DbContext connInfo, object[] pkValues, Expression <Func <T, object> > fieldName, string tableName = "", bool forUpdate = false)
 => SingleValuePrimitive <T>(connInfo, pkValues, SmartCrudHelper.GetPropertyName <T>(fieldName), tableName, forUpdate);