예제 #1
0
        public DbStructContainer Modify <T>(T obj) where T : class, new()
        {
            string            query     = string.Empty;
            DbStructContainer container = TryGetTableFields(obj);

            if (container == null)
            {
                return(container);
            }
            container.OperateType = 3;
            DbFieldBase[] oFields = container.SQLFields;

            string[] keyvalues = new string[oFields.Length];
            for (int i = 0; i < oFields.Length; i++)
            {
                DbFieldBase oBaseField = oFields[i];
                string      sValue     = oBaseField.Value == null ? string.Empty : oBaseField.Value.ToString();
                sValue       = sValue.Replace("\"", "__SY__"); // 为了防止数据内容因为引号导致无法输入, 故此进行引号转换
                keyvalues[i] = string.Format("{0}=\"{1}\"", oBaseField.FieldName, sValue);
            }
            string sKeyValues = string.Join(",", keyvalues);

            container.SQLQuery = string.Format("update {0} set {1} where Guid=\"{2}\"", container.SQLTable, sKeyValues, container.SQLFields[container.SQLFields.Length - 1].Value);  // 基类的Guid在最后一位,故而这里使用.Length - 1
            return(container);
        }
예제 #2
0
        public DbStructContainer TryGetTableFields(object obj)
        {
            string             sSQLTable  = string.Empty;
            DbStructContainer  container  = null;
            List <DbFieldBase> oFieldList = new List <DbFieldBase>();

            Type type = obj.GetType();

            object[] oSaveDBAttributes = type.GetCustomAttributes(typeof(DbAttribute), false);
            if (oSaveDBAttributes.Length > 0)
            {
                sSQLTable = (oSaveDBAttributes[0] as DbAttribute).TableName;
                FieldInfo[] fieldInfos = type.GetFields();
                for (int i = 0; i < fieldInfos.Length; i++)
                {
                    FieldInfo fieldInfo = fieldInfos[i];
                    object[]  oFieldUnSaveDBAttributes = fieldInfo.GetCustomAttributes(typeof(DbUnsaveAttribute), false);
                    if (oFieldUnSaveDBAttributes.Length == 0)
                    {
                        DbFieldBase oField = mConvert.Type2Object(fieldInfo.FieldType);
                        oField.FieldName = fieldInfo.Name;
                        oField.Value     = fieldInfo.GetValue(obj);
                        oFieldList.Add(oField);
                    }
                }
                container           = new DbStructContainer();
                container.SQLTable  = sSQLTable;
                container.SQLFields = oFieldList.ToArray();
            }
            return(container);
        }
예제 #3
0
        public DbStructContainer Insert <T>(T obj) where T : class, new()
        {
            string            query     = string.Empty;
            DbStructContainer container = TryGetTableFields(obj);

            if (container == null)
            {
                return(container);
            }
            container.OperateType = 2;
            DbFieldBase[] oFields = container.SQLFields;
            string[]      fields  = new string[oFields.Length];
            string[]      values  = new string[oFields.Length];
            for (int i = 0; i < oFields.Length; i++)
            {
                DbFieldBase oBaseField = oFields[i];
                fields[i] = oBaseField.FieldName;
                string sValue = oBaseField.Value == null ? string.Empty : oBaseField.Value.ToString();
                sValue    = sValue.Replace("\"", "__SY__"); // 为了防止数据内容因为引号导致无法输入, 故此进行引号转换
                values[i] = "\"" + sValue + "\"";
            }
            string sFields = string.Join(",", fields);
            string sValues = string.Join(",", values);

            container.SQLQuery = string.Format("insert into {0}({1}) values ({2})", container.SQLTable, sFields, sValues);
            return(container);
        }
예제 #4
0
        public override void OnReader(DatabaseReader reader)
        {
            IList <T> list = new List <T>();

            if (reader == null)
            {
                return;
            }

            DbStructure.DbStructContainer[] containers = new DbStructure.DbStructContainer[reader.ElementCount];
            for (int i = 0; i < containers.Length; i++)
            {
                DbStructure.DbStructContainer tmpConatiner = new DbStructure.DbStructContainer();
                tmpConatiner.CopyFrom(Container);
                containers[i] = tmpConatiner;
            }

            int index = 0;

            foreach (var item in reader)
            {
                DbStructure.DbStructContainer  tmpContainer = containers[index];
                DatabaseReader.DatabaseElement oElement     = item as DatabaseReader.DatabaseElement;
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    tmpContainer.SQLFields[i].Value = oElement.GetValue(i);
                }
                index = index + 1;
            }

            for (int tmpIndex = 0; tmpIndex < containers.Length; tmpIndex++)
            {
                DbStructure.DbStructContainer tmpContainer = containers[tmpIndex];
                T           t          = new T();
                FieldInfo[] fieldInfos = t.GetType().GetFields();
                for (int i = 0; i < fieldInfos.Length; i++)
                {
                    FieldInfo   fieldInfo  = fieldInfos[i];
                    DbFieldBase oBaseField = SearchField(tmpContainer.SQLFields, fieldInfo.Name);
                    if (oBaseField != null)
                    {
                        try
                        {
                            fieldInfo.SetValue(t, Convert.ChangeType(oBaseField.Value, fieldInfo.FieldType));
                        }
                        catch (Exception e)
                        {
                            App.Error("设置参数出现异常! 表{0} 索引位置{1}, 列名{2} 异常:{3}", Container.SQLTable, i, oBaseField.FieldName, e);
                        }
                    }
                }
                list.Add(t);
                Obj = list;
            }
        }
예제 #5
0
 public void CopyFrom(DbStructContainer container)
 {
     SQLTable  = container.SQLTable;
     SQLQuery  = container.SQLQuery;
     SQLFields = new DbFieldBase[container.SQLFields.Length];
     for (int i = 0; i < SQLFields.Length; i++)
     {
         DbFieldBase oBaseField = new DbFieldBase();
         oBaseField.CopyFrom(container.SQLFields[i]);
         SQLFields[i] = oBaseField;
     }
 }
예제 #6
0
 protected DbFieldBase SearchField(DbFieldBase[] oBaseFields, string oCondName)
 {
     for (int i = 0; i < oBaseFields.Length; i++)
     {
         DbFieldBase oBaseField = oBaseFields[i];
         if (oBaseField.FieldName.Equals(oCondName))
         {
             return(oBaseField);
         }
     }
     return(null);
 }
예제 #7
0
        public DbFieldBase Type2Object(Type fieldType)
        {
            DbFieldBase oField = null;

            if (fieldType == typeof(Boolean))
            {
                oField = new DbBooleanField();
            }
            else if (fieldType == typeof(Int16))
            {
                oField = new DbInt16Field();
            }
            else if (fieldType == typeof(Int32))
            {
                oField = new DbInt32Field();
            }
            else if (fieldType == typeof(Int64))
            {
                oField = new DbInt64Field();
            }
            else if (fieldType == typeof(UInt16))
            {
                oField = new DbUInt16Field();
            }
            else if (fieldType == typeof(UInt32))
            {
                oField = new DbUInt32Field();
            }
            else if (fieldType == typeof(UInt64))
            {
                oField = new DbUInt64Field();
            }
            else if (fieldType == typeof(Single))
            {
                oField = new DbSingleField();
            }
            else if (fieldType == typeof(Double))
            {
                oField = new DbDoubleField();
            }
            else if (fieldType == typeof(String))
            {
                oField = new DbStringField();
            }
            return(oField);
        }
예제 #8
0
        public DbFieldBase Type2Object(string strType)
        {
            DbFieldBase oField = null;

            if (strType.Equals(typeof(Boolean).Name))
            {
                oField = new DbBooleanField();
            }
            else if (strType.Equals(typeof(Int16).Name))
            {
                oField = new DbInt16Field();
            }
            else if (strType.Equals(typeof(Int32).Name))
            {
                oField = new DbInt32Field();
            }
            else if (strType.Equals(typeof(Int64).Name))
            {
                oField = new DbInt64Field();
            }
            else if (strType.Equals(typeof(UInt16).Name))
            {
                oField = new DbUInt16Field();
            }
            else if (strType.Equals(typeof(UInt32).Name))
            {
                oField = new DbUInt32Field();
            }
            else if (strType.Equals(typeof(UInt64).Name))
            {
                oField = new DbUInt64Field();
            }
            else if (strType.Equals(typeof(Single).Name))
            {
                oField = new DbSingleField();
            }
            else if (strType.Equals(typeof(Double).Name))
            {
                oField = new DbDoubleField();
            }
            else if (strType.Equals(typeof(String).Name))
            {
                oField = new DbStringField();
            }
            return(oField);
        }
예제 #9
0
 public override void OnReader(DatabaseReader reader)
 {
     if (reader == null)
     {
         Obj = default(T);
     }
     if (reader.ElementCount <= 0)
     {
         Obj = default(T);
     }
     foreach (var item in reader)
     {
         DatabaseReader.DatabaseElement oElement = item as DatabaseReader.DatabaseElement;
         for (int i = 0; i < reader.FieldCount; i++)
         {
             Container.SQLFields[i].Value = oElement.GetValue(i);
         }
         break;  // Search 限定只查询一个数据,因此这里执行一次后直接返回
     }
     FieldInfo[] fieldInfos = Obj.GetType().GetFields();
     for (int i = 0; i < fieldInfos.Length; i++)
     {
         FieldInfo   fieldInfo  = fieldInfos[i];
         DbFieldBase oBaseField = SearchField(Container.SQLFields, fieldInfo.Name);
         if (oBaseField != null)
         {
             try
             {
                 fieldInfo.SetValue(Obj, Convert.ChangeType(oBaseField.Value, fieldInfo.FieldType));
             }
             catch (Exception e)
             {
                 App.Error("设置参数出现异常! 表{0} 索引位置{1}, 列名{2} 异常:{3}", Container.SQLTable, i, oBaseField.FieldName, e);
             }
         }
     }
 }
예제 #10
0
 public void CopyFrom(DbFieldBase oBaseField)
 {
     FieldName = oBaseField.FieldName;
     TypeName  = oBaseField.TypeName;
     Value     = oBaseField.Value;
 }