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

            //is this a new record? If so, we need an INSERT statement. Else, an UPDATE
            if (NewOrOld == true)
            {
                queryFull = "INSERT INTO " + tableName + " (";
                //the numerical ID increments for us, so we just need to state which fields we're inserting into
                int i = 0;
                foreach (String att in attribNames)
                {
                    if (i > 0 && i<attribNames.Count()-1)
                    {
                        queryFull += att + ", ";
                    }
                    if (i == attribNames.Count()-1)
                    {
                        queryFull += att + ") ";
                    }
                    i++;
                }
                queryFull += "VALUES (";
                queryFull += "'" + workTicketCreator + "', ";
                queryFull += "'" + workTicketLocation + "', ";
                queryFull += "'" + workTicketAllocatedTo + "', ";
                queryFull += "'" + workTicketTitle + "', ";
                queryFull += "'" + workTicketDesc + "', ";
                queryFull += "'" + Convert.ToString(workTicketApprovalStatus) + "', ";
                queryFull += "'" + Convert.ToString(workTicketActive) + "', ";

                queryFull += "'" + createdTimeStamp.ToString("dd-MMMM-yyyy H:mm:ss") + "')";
            }
            else
            {
                //Verify record exists
                WorkTicket test = new WorkTicket();
                int aTest = test.Load(workTicketID);
                if (aTest < 0) { return -1; }

                //build the UPDATE query (clientName is skipped because it's a primary key)
                queryFull = "UPDATE " + tableName + " SET ";
                queryCondition = "WHERE " + attribNames[0] + "=" + workTicketID;

                queryFull += attribNames[1] + "='" + workTicketCreator + "', ";
                queryFull += attribNames[2] + "='" + workTicketLocation + "', ";
                queryFull += attribNames[3] + "='" + workTicketAllocatedTo + "', ";
                queryFull += attribNames[4] + "='" + workTicketTitle + "', ";
                queryFull += attribNames[5] + "='" + workTicketDesc + "', ";
                queryFull += attribNames[6] + "='" + Convert.ToString(workTicketApprovalStatus) + "', ";
                queryFull += attribNames[7] + "='" + Convert.ToString(workTicketActive) + "', ";

                queryFull += attribNames[8] + "='" + 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; }

            NewOrOld = false;   //fail-safe

            return 0;
        }
Exemple #2
0
        public int Populate(String fieldName, String criteria)
        {
            //re-initialise lists
            //initialise lists
            workTicketList = new List<WorkTicket>();
            workTicketIDList = new List<int>();

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

            //verify fieldname is valid - if it isn't, assume custom WHERE condition
            Boolean fieldTest = false;
            for (int i = 0; i <= numAttribs; i++)
            {
                if (attribNames[i] == fieldName)
                {
                    fieldTest = true;
                    break;
                }
            }
            if (!fieldTest) { queryCondition += criteria; }
            else
            {
                queryCondition += fieldName + "=" + criteria;
            }
            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;
            }

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

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

            //Load found bookings into list
            foreach (int id in workTicketIDList)
            {
                WorkTicket newWorkTicket = new WorkTicket();
                int loadWorkTicket = newWorkTicket.Load(id);
                if (loadWorkTicket < 0) { return -1; }
                else
                {
                    workTicketList.Add(newWorkTicket);
                }
            }

            //verify that more than one booking has been added
            if (workTicketList.Count() < 1) { return -1; }

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