Пример #1
0
        public int Save()
        {
            //Strings to handle query as it's built
            String queryFull = "", queryCondition = "";

            //verify input username is valid
            if (resourceName == "" || resourceName == "*" || resourceName == " ") { return -1; }

            //is this a new record? If so, we need an INSERT statement. Else, an UPDATE
            if (NewOrOld == true)
            {
                queryFull = "INSERT INTO " + tableName + " VALUES (";
                queryFull += "'" + resourceName + "', ";
                queryFull += "'" + resourceDesc + "', ";
                queryFull += resourceQuantity + ", ";   //no quotes needed around numeric values
                queryFull += "'" + Convert.ToString(resourceActive)+ ", ";
                queryFull += resourceRate + ", ";
                queryFull += "'" + createdTimeStamp.ToString("dd-MMMM-yyyy H:mm:ss") + "')";
            }
            else
            {
                //verify the record actually exists
                ResourceRecord test = new ResourceRecord();
                int initCheck = test.Load(resourceName);
                if (initCheck < 0) { return -1; }

                //build the UPDATE query (resourceName is skipped because it's a primary key)
                queryFull = "UPDATE " + tableName + " SET ";
                queryCondition = "WHERE " + attribNames[0] + "='" + resourceName + "'";
                queryFull += attribNames[1] + "='" + resourceDesc + "', ";
                queryFull += attribNames[2] + "=" + resourceQuantity + ", ";
                queryFull += attribNames[3] + "='" + Convert.ToString(resourceActive)  + "', ";
                queryFull += attribNames[4] + "=" + resourceRate + ", ";
                queryFull += attribNames[5] + "='" + createdTimeStamp.ToString("dd-MMMM-yyyy H:mm:ss") + "' ";
                queryFull += queryCondition;
            }

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }

            //check if saved correctly if new
            if (NewOrOld == true)
            {
                int saveCheck = Load(resourceName);
                if (saveCheck < 0) { return -1; }
            }

            NewOrOld = false;   //fail-safe
            return 0;
        }
Пример #2
0
        public int Save()
        {
            //Strings to handle query as it's built
            String queryFull = "", queryCondition = "";
            int result = 0;

            //verify input keys
            if (BookingID < 0) { return -1; }

            /* TO-DO: UNCOMMENT ONCE BOOKING CLASS EXISTS */
            //Booking test = new Booking();
            //result = test.Load(BookingID);

            if (ResName == "" || ResName == "*" || ResName == " ") { return -1; }
            ResourceRecord test = new ResourceRecord();
            result = test.Load(ResName);
            if (result < 0) { return -1; }

            //is this a new record? If so, we need an INSERT statement. Else, an UPDATE
            if (NewOrOld == true)
            {
                queryFull = "INSERT INTO " + tableName + " VALUES (";
                queryFull += BookingID + ", ";
                queryFull += "'" + ResName + "', ";
                queryFull += "'" + Requester + "', ";
                queryFull += Quantity + ", ";
                queryFull += "'" + Convert.ToString(Approved) + "', ";

                queryFull += "'" + createdTimeStamp.ToString("dd-MMMM-yyyy H:mm:ss") + "')";
            }
            else
            {
                //verify the record actually exists
                ResourceRequest testA = new ResourceRequest();
                int initCheck = testA.Load(BookingID, ResName);
                if (initCheck < 0) { return -1; }

                //build the UPDATE query (the two primary key fields are skipped here)
                queryFull = "UPDATE " + tableName + " SET ";
                queryCondition = "WHERE " + attribNames[0] + "=" + BookingID + " AND " + attribNames[1] + "='" + ResName + "'"; //compound key!
                queryFull += attribNames[2] + "='" + Requester + "', ";
                queryFull += attribNames[3] + "=" + Quantity + ", ";
                queryFull += attribNames[4] + "='" + Convert.ToString(Approved) + "', ";

                queryFull += attribNames[5] + "='" + createdTimeStamp.ToString("dd-MMMM-yyyy H:mm:ss") + "' ";
                queryFull += queryCondition;
            }

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }

            //if new record, verify it saved correctly
            if (NewOrOld == true)
            {
                int saveCheck = Load(BookingID, ResName);
                if (saveCheck < 0) { return -1; }
            }

            NewOrOld = false;   //fail-safe
            return 0;
        }
Пример #3
0
        public int Delete()
        {
            //Use with caution! This will fail if orphaned entries exist
            String queryFull = "DELETE FROM " + tableName, queryCondition = " WHERE " + attribNames[0] + "='" + resourceName + "'";

            //verify input username is valid
            if (resourceName == "" || resourceName == "*" || resourceName == " ") { return -1; }

            //Check record exists in the first place
            ResourceRecord test = new ResourceRecord();
            int initCheck = test.Load(resourceName);
            if (initCheck < 0) { return -1; }

            /* TO DO - UNCOMMENT AND TEST LINES BELOW */
            //Verify there are no resource requests (i.e. does it have entries that can be orphaned? If so, don't delete)
            //ResourceRequest rsrNew = New ResourceRequest();
            //int rsrCheck = rsrNew.Search(1, resourceName);
            //if(rsrCheck > -1) { return -1; }

            queryFull += queryCondition; //pull query together

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //Check was deleted successfully
            int delCheck = Load(resourceName);
            if (delCheck > -1) { return -1; }

            NewOrOld = true;    //as the record no longer exists in the database, is considered 'new'

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }

            return 0;
        }
Пример #4
0
        public int Populate(String fieldName, String criteria)
        {
            //re-initialise list
            resList = new List<ResourceRecord>();
            resNames = new List<String>();

            //Build query - only need a list of IDs to know which bookings to populate list with
            String queryFull = "SELECT " + attribNames[0] + " FROM " + tableName + " WHERE " + fieldName + "=" + criteria;

            //verify fieldname is valid
            Boolean fieldTest = false;
            for (int i = 0; i <= numAttribs; i++)
            {
                if (attribNames[i] == fieldName)
                {
                    fieldTest = true;
                    break;
                }
            }
            if (!fieldTest) { return -1; }

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //read in all the results and add them to list of booking IDs
            while (dbReader.Read())
            {
                try
                {
                    resNames.Add(dbReader[attribNames[0]].ToString());
                }
                catch { return -1; }
            }

            //verify at least one result was returned, else error
            if (resNames.Count() < 1) { return -1; }

            //Load found bookings into list
            foreach (String res in resNames)
            {
                ResourceRecord newResRecord = new ResourceRecord();
                int loadNewResRecord = newResRecord.Load(res);
                if (loadNewResRecord < 0) { return -1; }
                else
                {
                    resList.Add(newResRecord);
                }
            }

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }
            return 0;
        }