public static bool existObj(object obj)
        {
            List <string> prikey = new List <string>(), prival = new List <string>();

            DBAttribute.GetDBPrimaryElement(obj.GetType(), obj, prikey, prival);
            return(exist(obj.GetType(), prikey, prival));
        }
Example #2
0
        public static bool Delete(OracleConnection connection, object obj)
        {
            Type          type   = obj.GetType();
            StringBuilder strSql = new StringBuilder();

            try
            {
                strSql.Append(string.Format("delete from {0}", DBAttribute.GetDBTable(type)));
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException(e.Message);
                //   throw new ArgumentException("Invaild Argument!\n\n from Delete(OracleConnection connection, object obj) \n");
            }
            List <string> propertyPrimaryKeyList   = new List <string>();
            List <string> propertyPrimaryValueList = new List <string>();

            DBAttribute.GetDBPrimaryElement(type, obj, propertyPrimaryKeyList, propertyPrimaryValueList);
            if (!(propertyPrimaryKeyList.Any() && propertyPrimaryValueList.Any()))
            {
                throw new ArgumentException("Invaild Argument!\n\n from Delete(OracleConnection connection, object obj) \n");
            }
            strSql.Append(string.Format(" where "));
            for (int i = 0; i < propertyPrimaryKeyList.Count; ++i)
            {
                strSql.Append(string.Format(" {0}={1} AND", propertyPrimaryKeyList[i], propertyPrimaryValueList[i]));
            }
            strSql.Length -= 3;
            return(ExecuteSql(connection, strSql.ToString()) > 0);
        }
Example #3
0
        public static bool Update(OracleConnection connection, object obj)
        {
            //不允许更改主码
            StringBuilder strSql = new StringBuilder();
            List <string> key = new List <string>(), pri = new List <string>();
            Dictionary <string, OracleParameter> opd = GetDBMember(obj, key, pri);
            List <OracleParameter> op = new List <OracleParameter>();

            strSql.Append(string.Format("update {0} set ", DBAttribute.GetDBTable(obj.GetType())));
            for (int i = 0; i < key.Count(); ++i)
            {
                strSql.Append(string.Format(" {0} = :{0} ,", key[i]));
                op.Add(opd[key[i]]);
            }
            --strSql.Length;
            strSql.Append(" where ");
            for (int i = 0; i < pri.Count(); ++i)
            {
                strSql.Append(string.Format(" {0} = :{0} AND", pri[i]));
                op.Add(opd[pri[i]]);
            }
            strSql.Length -= 3;

            return(ExecuteSql(connection, strSql.ToString(), op.ToArray()) > 0);
        }
Example #4
0
        public static void Query(OracleConnection connection, object obj, List <string> needs, List <string> res)
        {
            StringBuilder StrSql = new StringBuilder();                 //字符串拼接器

            try
            {
                StrSql.Append(string.Format("select"));//DBAttribute.GetDBTable(obj.GetType()))
                for (int i = 0; i < needs.Count; i++)
                {
                    if (i == 0)
                    {
                        StrSql.Append(string.Format(" {0}", needs[i]));
                    }
                    else
                    {
                        StrSql.Append(string.Format(",{0}", needs[i]));
                    }
                }
                StrSql.Append(string.Format(" from {0}", GetDBTable(obj.GetType())));//DBAttribute.GetDBTable(obj.GetType()))

                List <string> propertyPrimaryKeyList   = new List <string>();
                List <string> propertyPrimaryValueList = new List <string>();
                //DBAttribute.GetDBElement(obj.GetType(), obj, propertyPrimaryKeyList, propertyPrimaryValueList);
                DBAttribute.GetDBPrimaryElement(obj.GetType(), obj, propertyPrimaryKeyList, propertyPrimaryValueList);
                if (!(propertyPrimaryKeyList.Any() && propertyPrimaryValueList.Any()))
                {
                    throw new ArgumentException("Invaild Argument!\n\n from Delete(OracleConnection connection, object obj) \n");
                }
                StrSql.Append(string.Format(" where "));

                for (int i = 0; i < propertyPrimaryKeyList.Count; ++i)
                {
                    if (propertyPrimaryValueList[i] != null)
                    {
                        StrSql.Append(string.Format(" {0}={1} AND", propertyPrimaryKeyList[i], propertyPrimaryValueList[i]));
                    }
                }
                StrSql.Length -= 3;              //减去AND
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException(e.Message);
            }
            OracleCommand cmd = new OracleCommand();

            PrepareCommand(cmd, connection, null, StrSql.ToString(), null);
            OracleDataReader odr = null;

            odr = cmd.ExecuteReader();
            //odr读取数据并存入res
            while (odr.Read())
            {
                for (int i = 0; i < needs.Count; i++)
                {
                    res.Add(odr[i].ToString());
                }
            }
        }
        public static bool exist(Type type, List <string> key, List <string> val)
        {
            OracleConnection conn = pool.fetchConnection();
            StringBuilder    sb   = new StringBuilder();

            sb.Append(string.Format("select * from {0} where ", DBAttribute.GetDBTable(type)));
            int len = key.Count();

            for (int i = 0; i < len; ++i)
            {
                if (i != 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append($"{key[i]}='{val[i]}'");
            }
            bool res = OracleHelper.Exists(conn, sb.ToString());

            pool.releaseConnection(conn);
            return(res);
        }