예제 #1
0
        // Method for inserting data into database
        public bool Insert(ContactClass c)
        {
            // Create default type and set its value to false
            bool isSuccess = false;

            // 1. Connect to db
            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                // 2. Create Sql query to insert data into db
                string sql = "INSERT INTO tbl_contact (FirstName, LastName, ContactNo, Address, Gender) VALUES (@FirstName, @LastName, @ContactNo, @Address, @Gender)";

                // Create cmd using sql and conn
                SqlCommand cmd = new SqlCommand(sql, conn);

                // Create Parameters to add data
                cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("@LastName", c.LastName);
                cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo);
                cmd.Parameters.AddWithValue("@Address", c.Address);
                cmd.Parameters.AddWithValue("@Gender", c.Gender);

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

                // If the query runs successfuly then the value of rows will be > 0, else it will be = 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
            return(isSuccess);
        }
예제 #2
0
        // Method for updating data into database
        public bool Update(ContactClass c)
        {
            // Create default return type and set its default value to false
            bool isSuccess = false;

            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                // SQL to update data in our db
                string sql = "UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, ContactNo=@ContactNo, Address=@Address, Gender=@Gender WHERE ContactID=@ContactID";

                // Create SQL command
                SqlCommand cmd = new SqlCommand(sql, conn);

                // Create Parameters to add value
                cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("@LastName", c.LastName);
                cmd.Parameters.AddWithValue("@ContactNo", c.ContactID);
                cmd.Parameters.AddWithValue("@Address", c.Address);
                cmd.Parameters.AddWithValue("@Gender", c.Gender);
                cmd.Parameters.AddWithValue("@ContactID", c.ContactID);

                // Open DB connection
                conn.Open();
                int rows = cmd.ExecuteNonQuery();

                // If the query runs successfuly then the value of rows will be > 0, else it will be = 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
            return(isSuccess);
        }
예제 #3
0
        // Method for deleting data from database
        public bool Delete(ContactClass c)
        {
            // Create default return value and set its value to false
            bool isSuccess = true;

            // Create SQL connection
            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                // SQL to delete data
                string sql = "DELETE FROM tbl_contact WHERE ContactID=@ContactID";

                // Create SQL command
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@ContactID", c.ContactID);

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

                // If the query runs successfuly then the value of rows will be > 0, else it will be = 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                // Close connection
                conn.Close();
            }
            return(isSuccess);
        }