Beispiel #1
0
        private string getTableNameByVo(BaseVo baseVo)
        {
            string type  = baseVo.GetType().ToString();
            string table = type.Substring(type.LastIndexOf('.') + 1);

            return(table);
        }
Beispiel #2
0
        public List <BaseVo> getResults(BaseVo baseVo)
        {
            bool          needQuery          = false;
            List <BaseVo> result             = new List <BaseVo>();
            string        tableName          = getTableNameByVo(baseVo);
            Dictionary <string, object> dict = ObjectUtils.EachObjProperties(baseVo);
            string fields = "";

            string where = " where 1=1 ";

            List <string> fieldList = new List <string>();// 字段名列表,有序

            foreach (var prop in dict)
            {
                needQuery = true;
                Console.WriteLine(prop.Key, prop.Value);

                fields += prop.Key + ", ";
                fieldList.Add(prop.Key);

                Console.WriteLine(prop.Value);

                PropertyInfo pInfo = baseVo.GetType().GetProperty(prop.Key);

                if (pInfo.PropertyType == typeof(string))
                {
                    if (prop.Value != null && prop.Value.ToString() != "")
                    {
                        where += " and " + prop.Key + " = " + prop.Value.ToString();
                    }
                }
                else if (pInfo.PropertyType == typeof(int))
                {
                    if (int.Parse(prop.Value.ToString()) != 0)
                    {
                        where += " and " + prop.Key + " = " + prop.Value.ToString();
                    }
                }
            }

            if (!needQuery)
            {
                return(result);
            }

            fields = fields.Substring(0, fields.Length - 2);


            string sql = "select " + fields + " from " + tableName + where;

            string typeName = baseVo.GetType().ToString();
            Type   type     = Type.GetType(typeName);

            SQLiteDataReader sr = executeReader(sql);

            while (sr.Read())
            {
                var obj = type.Assembly.CreateInstance(typeName) as BaseVo;

                for (int i = 0; i < fieldList.Count; i++)
                {
                    string field = fieldList[i];
                    string value = sr[field].ToString();

                    PropertyInfo pInfo = obj.GetType().GetProperty(field);

                    Console.WriteLine(pInfo.PropertyType);
                    if (pInfo.PropertyType == typeof(string))
                    {
                        pInfo.SetValue(obj, value, null);
                    }
                    else if (pInfo.PropertyType == typeof(int))
                    {
                        pInfo.SetValue(obj, int.Parse(value), null);
                    }
                    else
                    {
                        pInfo.SetValue(obj, value, null);
                    }
                }

                result.Add(obj as BaseVo);
            }

            return(result);
        }