예제 #1
0
        //Update data in database
        public bool Update(ContactClass c)
        {
            // Connect to Postgres
            NpgsqlConnection conn         = new NpgsqlConnection(myConnectionString);
            bool             querySuccess = false;

            try
            {
                //Update contact inside the database
                string query = "UPDATE contacts SET lastName=@lastName, " +
                               "firstName=@firstName, phoneNumber=@phoneNumber, address=@address WHERE " +
                               "contactID=@contactID";
                //Create command using query string and connection
                NpgsqlCommand cmd = new NpgsqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@lastName", c.LastName);
                cmd.Parameters.AddWithValue("@firstName", c.FirstName);
                cmd.Parameters.AddWithValue("@phoneNumber", c.PhoneNumber);
                cmd.Parameters.AddWithValue("@address", c.Address);
                cmd.Parameters.AddWithValue("@contactID", c.ContactID);

                conn.Open();
                int rows = cmd.ExecuteNonQuery();

                //If the query runs successfully, the value of rows will be greater than 0, else it will be 0
                if (rows > 0)
                {
                    querySuccess = true;
                }
                else
                {
                    querySuccess = false;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
            return(querySuccess);
        }
예제 #2
0
        //Insert data into database
        public bool Insert(ContactClass c)
        {
            // Connect to Postgres
            NpgsqlConnection conn         = new NpgsqlConnection(myConnectionString);
            bool             querySuccess = false;

            try
            {
                //Insert new contact into the database
                string query = "INSERT INTO contacts (lastName, firstName, phoneNumber, address) VALUES(@lastName, @firstName, @phoneNumber, @address)";
                //Create command using query string and connection
                NpgsqlCommand cmd = new NpgsqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@lastName", c.LastName);
                cmd.Parameters.AddWithValue("@firstName", c.FirstName);
                cmd.Parameters.AddWithValue("@phoneNumber", c.PhoneNumber);
                cmd.Parameters.AddWithValue("@address", c.Address);

                conn.Open();
                int rows = cmd.ExecuteNonQuery();

                //If the query runs successfully, the value of rows will be greater than 0, else it will be 0
                if (rows > 0)
                {
                    querySuccess = true;
                }
                else
                {
                    querySuccess = false;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
            return(querySuccess);
        }
예제 #3
0
        //Delete data in database
        public bool Delete(ContactClass c)
        {
            // Connect to Postgres
            NpgsqlConnection conn         = new NpgsqlConnection(myConnectionString);
            bool             querySuccess = false;

            try
            {
                //Update contact inside the database
                string query = "DELETE FROM contacts WHERE contactID=@contactID";
                //Create command using query string and connection
                NpgsqlCommand cmd = new NpgsqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@contactID", c.ContactID);

                conn.Open();
                int rows = cmd.ExecuteNonQuery();

                //If the query runs successfully, the value of rows will be greater than 0, else it will be 0
                if (rows > 0)
                {
                    querySuccess = true;
                }
                else
                {
                    querySuccess = false;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
            return(querySuccess);
        }