Ejemplo n.º 1
0
        public static bool InsertObject(OracleConnection connection, object obj)
        {
            StringBuilder strSql = new StringBuilder();

            List <string> all = new List <string>();
            Dictionary <string, OracleParameter> opd = getDBMember(obj, null, null, all);
            List <OracleParameter> op = new List <OracleParameter>();

            try
            {
                strSql.Append(string.Format("insert into {0}", DBAttribute.getDBTable(obj.GetType())));
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException("Invaild Argument!\n\n from Insert(OracleConnection connection,object obj) \n");
            }

            strSql.Append(string.Format("( {0} )", string.Join(",", all.ToArray())));
            strSql.Append(string.Format(" values ({0})", ":" + string.Join(",:", all.ToArray())));
            foreach (string str in all)
            {
                op.Add(opd[str]);
            }
            OracleCommand cmd = new OracleCommand();

            PrepareCommand(cmd, connection, null, strSql.ToString(), op.ToArray());
            int tem = cmd.ExecuteNonQuery();

            return(tem != -1 && tem != 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 删除obj对象
        /// </summary>
        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("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);
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public static T getObject <T>(T Obj)
        {
            OracleConnection conn = pool.fetchConnection();
            StringBuilder    sb   = new StringBuilder();

            sb.Append(string.Format("select * from {0} where ", DBAttribute.getDBTable(typeof(T))));
            List <string> key = new List <string>(), val = new List <string>();

            DBAttribute.getDBPrimaryElement(typeof(T), Obj, key, val);
            int len = key.Count();

            for (int i = 0; i < len; ++i)
            {
                sb.Append(string.Format(" {0}={1} AND ", key[i], val[i]));
            }
            sb.Length -= 4;
            List <T> lt  = testQuery <T>(sb.ToString());
            T        res = lt.Count() == 0?Obj:lt[0];

            pool.releaseConnection(conn);
            return(res);
        }
Ejemplo n.º 6
0
        /*不负责检查原对象是否存在于表中*/
        /// <summary>
        /// 修改
        /// </summary>
        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);
        }