예제 #1
0
        public static void Load(object o, string action, Connection trans)
        {
            PersistableAttribute info = Class.GetPersistenceInfo(o);

            //if (info.UseDirectSql)
            //{
            //    LoadViaDirectSql(o, trans);
            //    return;
            //}

            string spName = Database.SpPrefix + info.DataSource + "_Load" + action;

            SqlCommand cmd = GetCommand(spName, Database._ConnectionString);

            //SqlCommand cmd = GetCommand(spName, trans.DbConnection.ConnectionString);
            Database.MapParameterValues(o, cmd);

            cmd.Connection  = (SqlConnection)trans.DbConnection;
            cmd.Transaction = (SqlTransaction)trans.DbTransaction;
            using (SqlDataReader reader = cmd.ExecuteReader())
                if (reader.Read())
                {
                    Database.MapToInstance(reader, o);
                }
                else
                {
                    throw new ApplicationException(String.Format("No record found in {0} where {1}.", info.DataSource, Database.DisplayParameterValues(cmd.Parameters)));
                }
        }
예제 #2
0
        public static PersistableAttribute GetPersistenceInfo(this object o)
        {
            if (o == null)
            {
                throw new ApplicationException("PersistenceInfo is not available on null instances.");
            }

            Type t = o.GetType();

            foreach (Attribute a in t.GetCustomAttributes(typeof(PersistableAttribute), false))
            {
                PersistableAttribute pa = a as PersistableAttribute;
                pa.Instance = o;
                return(pa);
            }
            throw new ApplicationException(String.Format("The class {0} does not have a PersistableAttribute declared.", t.Name));
        }
예제 #3
0
        public static bool Delete(object o, string action, Connection trans)
        {
            PersistableAttribute info = Class.GetPersistenceInfo(o);
            //if (info.UseDirectSql)
            //    return DeleteViaDirectSql(o, trans);

            string spName = Database.SpPrefix + info.DataSource + "_Delete" + action;

            using (SqlCommand cmd = GetCommand(spName, Database._ConnectionString)) // trans.DbConnection.ConnectionString))
            {
                Database.MapParameterValues(o, cmd);

                cmd.Connection  = (SqlConnection)trans.DbConnection;
                cmd.Transaction = (SqlTransaction)trans.DbTransaction;
                return(cmd.ExecuteNonQuery() > 0);
            }
        }
예제 #4
0
        public static int Store(object o, string action, Connection cn)
        {
            PersistableAttribute pi = Class.GetPersistenceInfo(o);
            //if (pi.UseDirectSql)
            //    return StoreViaDirectSql(o, cn);

            string spName = Database.SpPrefix + pi.DataSource + "_Store" + action;

            using (SqlCommand cmd = GetCommand(spName, Database._ConnectionString)) // cn.DbConnection.ConnectionString))
            {
                Database.MapParameterValues(o, cmd);

                int rowsAffected = 0;
                cmd.Connection  = (SqlConnection)cn.DbConnection;
                cmd.Transaction = (SqlTransaction)cn.DbTransaction;
                rowsAffected    = cmd.ExecuteNonQuery();

                //set output parameter on object (eg. identity key)
                foreach (SqlParameter p in cmd.Parameters)
                {
                    if (p.ParameterName.ToLower() == "@" + pi.PrimaryKeyName.ToLower() &&
                        p.Direction == ParameterDirection.Input)
                    {
                        throw new ApplicationException("Primary key must be set as an output parameter in the stored procedure: " + spName);
                    }
                    if (p.Direction != ParameterDirection.Input)
                    {
                        string name = p.ParameterName;
                        if (name.StartsWith("@"))
                        {
                            name = name.Substring(1);
                        }

                        if (o.FieldExists("_" + name))
                        {
                            o.SetFieldValueByName("_" + name, p.Value);
                        }
                        else
                        {
                            o.SetPropertyValueByName(name, p.Value);
                        }
                    }
                }
                return(rowsAffected);
            }
        }