Пример #1
0
        /// <summary>
        /// DataReader 转换为 Model
        /// </summary>
        /// <param name="idr"></param>
        /// <returns></returns>
        protected virtual T DataReaderToModel(IDataReader idr)
        {
            if (null == idr)
            {
                return(null);
            }
            if (idr.Read())
            {
                T model = new T();
                PropertyInfo[] arrPInfo = model.GetType().GetProperties();
                foreach (PropertyInfo PInfo in arrPInfo)
                {
                    if (!idr.HasField(PInfo.Name))
                    {
                        continue;
                    }

                    if (DBNull.Value != idr[PInfo.Name])
                    {
                        PInfo.SetValue(model, idr[PInfo.Name], null);
                    }
                }
                return(model);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        // To process all types that are not defined in System.TypeCode
        private void SetValueForObjects(object data, string argValue)
        {
            switch (PInfo.PropertyType.FullName)
            {
            case "System.Uri":
                PInfo.SetValue(data, GetObjectValue <Uri>(argValue,
                                                          (str) => { return(new Uri(str)); }));
                break;

            case "System.Version":
                PInfo.SetValue(data, GetComparableValue <Version>(argValue,
                                                                  (str) => { return(new Version(str)); }));
                break;

            case "System.Byte[]":
                PInfo.SetValue(data, GetObjectValue <byte[]>(argValue,
                                                             (str) => { return(Encoding.ASCII.GetBytes(str)); }));
                break;

            case "System.Char[]":
                PInfo.SetValue(data, GetObjectValue <char[]>(argValue,
                                                             (str) => { return(str.ToArray <char>()); }));
                break;

            default:
                throw new NotSupportedException(string.Format("The type of the argument '{0}' is not supported.", NamedArgument.Name));
            }
        }
Пример #3
0
        public bool AddModel(T model)
        {
            bool   isSuccess = false;
            string cmdtext   = model.AddSQL;

            try
            {
                if (model.PKeyDataType.IsDigitType())
                {
                    object obj = DbHelperSQL.GetSingle(cmdtext, model.ParamsForAdd);

                    if (null != obj && DBNull.Value != obj && obj.ToString() != "0")
                    {
                        PropertyInfo[] arrPInfo = model.GetType().GetProperties();
                        foreach (PropertyInfo PInfo in arrPInfo)
                        {
                            if (PInfo.Name.Equals(PKey))
                            {
                                if (model.PKeyDataType == typeof(System.Int32))
                                {
                                    PInfo.SetValue(model, System.Int32.Parse(obj.ToString()), null);
                                }
                                else if ((model.PKeyDataType == typeof(System.Int64)))
                                {
                                    PInfo.SetValue(model, System.Int64.Parse(obj.ToString()), null);
                                }

                                break;
                            }
                        }
                        isSuccess = true;
                    }
                    else
                    {
                        isSuccess = false;
                    }
                }
                else
                {
                    if (DbHelperSQL.ExecuteSql(cmdtext, model.ParamsForAdd) > 0)
                    {
                        isSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                throw ex;
            }

            return(isSuccess);
        }
Пример #4
0
        // --------------------------------------------------------------------------------------------------
        #region Methods

        /// <summary>
        /// Set the Content
        /// </summary>
        /// <param name="value">new content</param>
        /// <param name="content">notify change on "Content" if true, on "Item" if false</param>
        public void Set(object value, bool content = true)
        {
            bool changed = !Object.Equals(Content, value);

            if (!changed && Type != null)
            {
                return;
            }

            LimeMsg.Debug("LimeProperty Content set: {0} = {1}", Ident, value);

            if (PInfo != null)
            {
                PInfo.SetValue(Source, value);
            }
            else
            {
                Source = value;

                if (Type == null)
                {
                    Type = value.GetType();
                }
                else if (value != null && value.GetType() != Type)
                {
                    throw new InvalidCastException(string.Format("LimeProperty Set Content: {0} (expected {1})", value.GetType(), Type));
                }
            }


            // Bind the new object
            if (changed && value is INotifyPropertyChanged src)
            {
                //LimeMsg.Debug("LimeProperty Set: Subscribe {0} / {1} ({2})", Ident, src, src.GetType());
                src.PropertyChanged += SourcePropertyChanged;
            }

            if (content)
            {
                OnPropertyChanged("Content");
                OnPropertyChanged("Value");
                OnPropertyChanged("Serialize");
            }
            else
            {
                OnPropertyChanged("Item");
            }
        }
Пример #5
0
        /// <summary>
        /// DataReader 转换为 ModelList
        /// </summary>
        /// <param name="idr"></param>
        /// <returns></returns>
        protected virtual List <T> DataReaderToList(IDataReader idr)
        {
            if (null == idr)
            {
                return(null);
            }
            List <T> modelList = new List <T>();

            while (idr.Read())
            {
                T model = new T();

                PropertyInfo[] arrPInfo = model.GetType().GetProperties();
                foreach (PropertyInfo PInfo in arrPInfo)
                {
                    if (!idr.HasField(PInfo.Name))
                    {
                        continue;
                    }
                    if (DBNull.Value != idr[PInfo.Name])
                    {
                        PInfo.SetValue(model, idr[PInfo.Name], null);
                    }
                }
                modelList.Add(model);
            }

            if (modelList.Count > 0)
            {
                return(modelList);
            }
            else
            {
                return(null);
            }
        }
Пример #6
0
        private void SetDirectPropertyValue(object data, string argValue)
        {
            TypeCode code = Type.GetTypeCode(PInfo.PropertyType);

            switch (code)
            {
            // Boolean values arguments do not have associated string values; if specified,
            // just set to true the associated property.
            case TypeCode.Boolean:
                PInfo.SetValue(data, true);
                break;

            case TypeCode.Byte:
                PInfo.SetValue(data, GetComparableValue <byte>(argValue, (str) => { return(Convert.ToByte(str)); }));
                break;

            case TypeCode.SByte:
                PInfo.SetValue(data, GetComparableValue <sbyte>(argValue, (str) => { return(Convert.ToSByte(str)); }));
                break;

            case TypeCode.Char:
                PInfo.SetValue(data, GetComparableValue <char>(argValue, (str) => { return(Convert.ToChar(str)); }));
                break;

            case TypeCode.Decimal:
                PInfo.SetValue(data, GetComparableValue <decimal>(argValue, (str) => { return(Convert.ToDecimal(str)); }));
                break;

            case TypeCode.Int16:
                PInfo.SetValue(data, GetComparableValue <short>(argValue, (str) => { return(Convert.ToInt16(str)); }));
                break;

            case TypeCode.Int32:
                PInfo.SetValue(data, GetComparableValue <int>(argValue, (str) => { return(Convert.ToInt32(str)); }));
                break;

            case TypeCode.Int64:
                PInfo.SetValue(data, GetComparableValue <long>(argValue, (str) => { return(Convert.ToInt64(str)); }));
                break;

            case TypeCode.UInt16:
                PInfo.SetValue(data, GetComparableValue <ushort>(argValue, (str) => { return(Convert.ToUInt16(str)); }));
                break;

            case TypeCode.UInt32:
                PInfo.SetValue(data, GetComparableValue <uint>(argValue, (str) => { return(Convert.ToUInt32(str)); }));
                break;

            case TypeCode.UInt64:
                PInfo.SetValue(data, GetComparableValue <ulong>(argValue, (str) => { return(Convert.ToUInt64(str)); }));
                break;

            case TypeCode.Single:
                PInfo.SetValue(data, GetComparableValue <float>(argValue, (str) => { return(Convert.ToSingle(str, CultureInfo.InvariantCulture)); }));
                break;

            case TypeCode.Double:
                PInfo.SetValue(data, GetComparableValue <double>(argValue, (str) => { return(Convert.ToDouble(str, CultureInfo.InvariantCulture)); }));
                break;

            case TypeCode.String:
                PInfo.SetValue(data, GetComparableValue <string>(argValue, (str) => { return(str); }));
                break;

            case TypeCode.DateTime:
                PInfo.SetValue(data, GetComparableValue <DateTime>(argValue, (str) => { return(Convert.ToDateTime(str, CultureInfo.InvariantCulture)); }));
                break;

            case TypeCode.Object:
                SetValueForObjects(data, argValue);
                break;

            default:
                throw new NotSupportedException(string.Format("The type of the argument '{0}' is not supported.", NamedArgument.Name));
            }
        }
Пример #7
0
        private void SetFileContentPropertyValue(object data, string argValue)
        {
            switch (PInfo.PropertyType.FullName)
            {
            case "System.String":
                PInfo.SetValue(data, GetObjectValue <string>(argValue,
                                                             (str) =>
                {
                    if (File.Exists(str) == false)
                    {
                        throw new FileNotFoundException(string.Format("The file path specified with the argument '{0}' does not exist.", NamedArgument.Name), str);
                    }

                    return(File.ReadAllText(str, FileContent.Encoding));
                }));
                break;

            case "System.String[]":
                PInfo.SetValue(data, GetObjectValue <string[]>(argValue,
                                                               (str) =>
                {
                    if (File.Exists(str) == false)
                    {
                        throw new FileNotFoundException(string.Format("The file path specified with the argument '{0}' does not exist.", NamedArgument.Name), str);
                    }

                    return(File.ReadAllLines(str, FileContent.Encoding));
                }));
                break;

            case "System.Byte[]":
                PInfo.SetValue(data, GetObjectValue <byte[]>(argValue,
                                                             (str) =>
                {
                    if (File.Exists(str) == false)
                    {
                        throw new FileNotFoundException(string.Format("The file path specified with the argument '{0}' does not exist.", NamedArgument.Name), str);
                    }
                    return(File.ReadAllBytes(str));
                }));
                break;

            case "System.Char[]":
                PInfo.SetValue(data, GetObjectValue <char[]>(argValue,
                                                             (str) =>
                {
                    if (File.Exists(str) == false)
                    {
                        throw new FileNotFoundException(string.Format("The file path specified with the argument '{0}' does not exist.", NamedArgument.Name), str);
                    }

                    return(File.ReadAllText(str, FileContent.Encoding).ToArray <char>());
                }));
                break;

            case "System.IO.FileStream":
                PInfo.SetValue(data, GetObjectValue <FileStream>(argValue,
                                                                 (str) =>
                {
                    if (File.Exists(str) == false)
                    {
                        throw new FileNotFoundException(string.Format("The file path specified with the argument '{0}' does not exist.", NamedArgument.Name), str);
                    }

                    return(File.OpenRead(str));
                }));
                break;

            default:
                throw new NotSupportedException(string.Format("The type of the argument '{0}' is not supported when associated with the CIFileContentAttribute attribute.", NamedArgument.Name));
            }
        }
Пример #8
0
        /// <summary>
        /// 批量插入实体数据,全部插入成功才返回True
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public bool AddModels(List <T> models, out List <T> errModels)
        {
            bool isSuccess = true;

            errModels = new List <T>();

            if (models != null && models.Count > 0)
            {
                SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString);
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                try
                {
                    if (models[0].PKeyDataType.IsDigitType())
                    {
                        foreach (T model in models)
                        {
                            comm.CommandText = model.AddSQL;
                            comm.Parameters.AddRange(model.ParamsForAdd);
                            object obj = comm.ExecuteScalar();
                            comm.Parameters.Clear();
                            if (null != obj && DBNull.Value != obj && obj.ToString() != "0")
                            {
                                PropertyInfo[] arrPInfo = model.GetType().GetProperties();
                                foreach (PropertyInfo PInfo in arrPInfo)
                                {
                                    if (PInfo.Name.Equals(PKey))
                                    {
                                        PInfo.SetValue(model, obj, null);

                                        break;
                                    }
                                }
                            }
                            else
                            {
                                errModels.Add(model);
                                isSuccess = false;
                            }
                        }
                    }
                    else
                    {
                        foreach (T model in models)
                        {
                            comm.CommandText = model.AddSQL;
                            comm.CommandType = CommandType.Text;
                            comm.Parameters.AddRange(model.ParamsForAdd);
                            if (comm.ExecuteNonQuery() <= 0)
                            {
                                errModels.Add(model);
                                isSuccess = false;
                            }
                            comm.Parameters.Clear();
                        }
                    }
                }
                catch
                {
                    isSuccess = false;
                    conn.Close();
                }
                comm.Dispose();
                if (conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
            return(isSuccess);
        }