/// <summary>
        /// Transforms a runtime value to the equivalent database value.
        /// This implementation returns the value without changing it.
        /// </summary>
        /// <param name="dbType">Database type.</param>
        /// <param name="value">Value to transform.</param>
        /// <returns>The transformed object.</returns>
        public virtual object TransformRuntimeToDatabaseValue(DbType dbType, object value)
        {
            if (value != null && value.Equals(String.Empty))
            {
                var numericTypes = new DbType[] {
                    DbType.Int16, DbType.Int32, DbType.Int64,
                    DbType.UInt16, DbType.UInt32, DbType.UInt64,
                    DbType.Byte, DbType.SByte
                };

                var decimalTypes = new DbType[] {
                    DbType.Currency, DbType.Decimal,
                    DbType.Single, DbType.Double,
                    DbType.VarNumeric
                };

                if (decimalTypes.Contains(dbType))
                {
                    return(new Decimal(0));
                }

                if (numericTypes.Contains(dbType))
                {
                    return(0);
                }
            }

            return(value);
        }
Ejemplo n.º 2
0
        internal static DataSet ExecuteSelect(EDtxReaderType connType, int timeout, IDbConnection conn, string cmdText, ParamHelper[] paramList, ref TimeSpan executionTime)
        {
            DateTime started = DateTime.Now;
            IDataReader reader = null;
            IDbCommand cmd = null;
            bool bConnOpenedHere = EnsureConnectionOpen(conn);
            IDbTransaction transaction = conn.BeginTransaction();
            DataSet retv = new DataSet();
            try
            {
                cmd = conn.CreateCommand();
                cmd.CommandTimeout = timeout;
                cmd.Transaction = transaction;
                cmd.CommandText = cmdText;
                DbType[] reqLenTypes = new DbType[] { DbType.AnsiString, DbType.AnsiStringFixedLength, DbType.String, DbType.StringFixedLength, DbType.Binary, DbType.Object, DbType.Xml };
                for (int i = 0; i < paramList.Length; i++)
                {
                    IDbDataParameter idb = cmd.CreateParameter();
                    cmd.Parameters.Add(HelperFunctions.BuildParameter(idb, paramList[i]));
                    if(reqLenTypes.Contains(idb.DbType))
                    {
                        if (idb is OdbcParameter)
                        {
                            (idb as OdbcParameter).Size = paramList[i].Size;
                        }
                        else if (idb is CtreeSqlParameter)
                        {
                            (idb as CtreeSqlParameter).Size = paramList[i].Size;
                        }
                    }
                }
                switch(connType)
                {
                    case EDtxReaderType.Adapter:
                    case EDtxReaderType.FaircomAdapter:
                        {
                            IDbDataAdapter adap = GetAdapter(connType, cmd);
                            retv.BeginInit();

                            //Since the FillSchema and Fill functions return the same instance of 'DataTable'
                            //There is probably a better way of doing this, but for sake of explination
                            //Read the db schema
                            bool bSchemaFound = false;
                            DataTable[] dta = adap.FillSchema(retv, SchemaType.Source);
                            DataTable clone = null;
                            if(dta.Length > 0)
                            {
                                bSchemaFound = true;
                                dta[0].TableName = "SchemaTable"; //Ensure the table is named 'SchemaTable'
                                retv.Tables.Remove(dta[0]); //Drop the table from the dataset
                                clone = dta[0].Clone(); //Clone the results
                                dta[0].TableName = "Table"; //Rename the datatable instance back to table
                            }
                            adap.Fill(retv); //Fill 'Table' with the actual results
                            if(bSchemaFound && clone != null)
                                retv.Tables.Add(clone); //Now add the 'schematable' back to the results

                            retv.EndInit();
                            break;
                        }
                    default:
                        {
                            DataTable dt;
                            reader = cmd.ExecuteReader();
                            if (reader.FieldCount > 0)
                            {
                                retv.Tables.Add(dt = new DataTable("Table"));
                                retv.Tables.Add(reader.GetSchemaTable());
                                switch (connType)
                                {
                                    case EDtxReaderType.FaircomReader:
                                    case EDtxReaderType.Reader:
                                        {
                                            dt.Load(reader, LoadOption.OverwriteChanges);
                                        }
                                        break;
                                    case EDtxReaderType.FaircomReaderSafe:
                                    case EDtxReaderType.ReaderSafe:
                                    default:
                                        {

                                            bool columnsBuilt = false;
                                            while (reader.Read())
                                            {
                                                if (columnsBuilt == false)
                                                {
                                                    BuildColumnData(dt, reader);
                                                    columnsBuilt = true;
                                                }
                                                AddDataRow(dt, reader);
                                            }
                                        }
                                        break;

                                }
                            }
                            break;
                        }
                }
                transaction.Commit();
                executionTime = DateTime.Now - started;
                //Now update parameter inputs with their respective values
                for (int i = 0; i < paramList.Length; i++)
                {
                    IDbDataParameter p = (IDbDataParameter)cmd.Parameters[i];
                    if(p.Direction != ParameterDirection.Input)
                    {
                        paramList[i].OutValue = p.Value;
                    }
                }
            }
            catch(Exception e)
            {
                transaction.Rollback();
                executionTime = DateTime.Now - started;
                HelperFunctions.LogException(e, false);
                throw e;
            }
            finally
            {
                if(reader != null)
                {
                    if(!reader.IsClosed)
                        reader.Close();
                    reader.Dispose();
                }
                if(cmd != null)
                {
                    cmd.Dispose();
                }
                if(bConnOpenedHere)
                {
                    conn.Close();
                }
            }

            return retv;
        }
Ejemplo n.º 3
0
        internal static DataSet ExecuteSelect(EDtxReaderType connType, int timeout, IDbConnection conn, string cmdText, ParamHelper[] paramList, ref TimeSpan executionTime)
        {
            DateTime       started         = DateTime.Now;
            IDataReader    reader          = null;
            IDbCommand     cmd             = null;
            bool           bConnOpenedHere = EnsureConnectionOpen(conn);
            IDbTransaction transaction     = conn.BeginTransaction();
            DataSet        retv            = new DataSet();

            try
            {
                cmd = conn.CreateCommand();
                cmd.CommandTimeout = timeout;
                cmd.Transaction    = transaction;
                cmd.CommandText    = cmdText;
                DbType[] reqLenTypes = new DbType[] { DbType.AnsiString, DbType.AnsiStringFixedLength, DbType.String, DbType.StringFixedLength, DbType.Binary, DbType.Object, DbType.Xml };
                for (int i = 0; i < paramList.Length; i++)
                {
                    IDbDataParameter idb = cmd.CreateParameter();
                    cmd.Parameters.Add(BuildParameter(idb, paramList[i]));
                    if (reqLenTypes.Contains(idb.DbType))
                    {
                        if (idb is OdbcParameter)
                        {
                            (idb as OdbcParameter).Size = paramList[i].Size;
                        }
                        else if (idb is CtreeSqlParameter)
                        {
                            (idb as CtreeSqlParameter).Size = paramList[i].Size;
                        }
                    }
                }
                switch (connType)
                {
                case EDtxReaderType.Adapter:
                case EDtxReaderType.FaircomAdapter:
                {
                    IDbDataAdapter adap = GetAdapter(connType, cmd);
                    retv.BeginInit();

                    //Since the FillSchema and Fill functions return the same instance of 'DataTable'
                    //There is probably a better way of doing this, but for sake of explination
                    //Read the db schema
                    bool        bSchemaFound = false;
                    DataTable[] dta          = adap.FillSchema(retv, SchemaType.Source);
                    DataTable   clone        = null;
                    if (dta.Length > 0)
                    {
                        bSchemaFound     = true;
                        dta[0].TableName = "SchemaTable";  //Ensure the table is named 'SchemaTable'
                        retv.Tables.Remove(dta[0]);        //Drop the table from the dataset
                        clone            = dta[0].Clone(); //Clone the results
                        dta[0].TableName = "Table";        //Rename the datatable instance back to table
                    }
                    adap.Fill(retv);                       //Fill 'Table' with the actual results
                    if (bSchemaFound && clone != null)
                    {
                        retv.Tables.Add(clone);         //Now add the 'schematable' back to the results
                    }
                    retv.EndInit();
                    break;
                }

                default:
                {
                    DataTable dt;
                    reader = cmd.ExecuteReader();
                    if (reader.FieldCount > 0)
                    {
                        retv.Tables.Add(dt = new DataTable("Table"));
                        retv.Tables.Add(reader.GetSchemaTable());
                        switch (connType)
                        {
                        case EDtxReaderType.FaircomReader:
                        case EDtxReaderType.Reader:
                        {
                            dt.Load(reader, LoadOption.OverwriteChanges);
                        }
                        break;

                        case EDtxReaderType.FaircomReaderSafe:
                        case EDtxReaderType.ReaderSafe:
                        default:
                        {
                            bool columnsBuilt = false;
                            while (reader.Read())
                            {
                                if (columnsBuilt == false)
                                {
                                    BuildColumnData(dt, reader);
                                    columnsBuilt = true;
                                }
                                AddDataRow(dt, reader);
                            }
                        }
                        break;
                        }
                    }
                    break;
                }
                }
                transaction.Commit();
                executionTime = DateTime.Now - started;
                //Now update parameter inputs with their respective values
                for (int i = 0; i < paramList.Length; i++)
                {
                    IDbDataParameter p = (IDbDataParameter)cmd.Parameters[i];
                    if (p.Direction != ParameterDirection.Input)
                    {
                        paramList[i].OutValue = p.Value;
                    }
                }
            }
            catch (Exception e)
            {
                transaction.Rollback();
                executionTime = DateTime.Now - started;
                HelperFunctions.LogException(e, false);
                throw e;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    reader.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (bConnOpenedHere)
                {
                    conn.Close();
                }
            }

            return(retv);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <returns></returns>
        public string Generate()
        {
            using (StringWriter stringWriter = new StringWriter())
            {
                //设置属性注释
                if (string.IsNullOrWhiteSpace(Comment?.CommentName) == false)
                {
                    stringWriter.WriteLine("/// <summary>");
                    stringWriter.WriteLine("/// " + Comment.CommentName);
                    stringWriter.WriteLine("/// </summary>");
                }
                if (IsProperty)
                {
                    if (IsGenerateAttribute)
                    {
                        var commentName = Comment?.CommentName;
                        if (string.IsNullOrWhiteSpace(commentName))
                        {
                            if (IsKey)
                            {
                                stringWriter.WriteLine("[Required]");
                            }
                        }
                        else
                        {
                            if (IsKey)
                            {
                                stringWriter.WriteLine($"[Required(ErrorMessage = \"{commentName}\")]");
                            }
#if NET5_0
                            stringWriter.WriteLine($"[Comment(\"{Comment.CommentName}\")]");
#endif
                        }

                        if (MaxLength > 0)
                        {
                            stringWriter.WriteLine($"[MaxLength({MaxLength})]");
                        }
                        if (MinLength > 0)
                        {
                            stringWriter.WriteLine($"[MinLength({MinLength})]");
                        }
                        if (string.IsNullOrEmpty(DbType) == false)
                        {
                            var max = MaxLength.ToString();
                            if (MaxLength == -1)
                            {
                                max = "max";
                            }
                            if (DbType.Contains("(") == false)
                            {
                                if (DbType.Contains("varchar"))
                                {
                                    DbType = DbType + "(" + max + ")";
                                }
                                else if (DbType.Contains("varbinary"))
                                {
                                    DbType += $"({max})";
                                }
                                else if (DbType.Contains("decimal"))
                                {
                                    if (DecimalDigits == null)
                                    {
                                        DecimalDigits = 2;
                                    }
                                    DbType = DbType + $"({MaxLength},{DecimalDigits})";
                                }
                            }
                            stringWriter.WriteLine($"[Column(TypeName = \"{DbType}\")]");
                        }
                    }


                    stringWriter.WriteLine($"{FiledLimit} {FieldTypeName} {FieldName}" + "{get;set;}");
                }
                else
                {
                    stringWriter.WriteLine($"{FiledLimit} {FieldTypeName} {FieldName};");
                }
                return(stringWriter.ToString());
            }
        }